Connect Using a Connection Pool

The NuoDB Node.js driver comes with a built-in connection pool.

Unlike a single connection, where an individual connection is established for each database interaction, a connection pool allows the application to reuse and share database connections rather than opening a new connection for each database operation.

This approach of connection pools reduces the overhead associated with repeatedly establishing and closing connections, resulting in improved performance, responsiveness, and scalability of the application in high concurrency scenarios.

To connect to the NuoDB database using a connection pool:

  1. Configure the connection pool

    Please review the shape of the pool configuration to customize for specific needs.

    {
        minAvailable: number,
        connectionConfig: ConnectionConfig,
        maxAge: number,
        checkTime: number,
        maxLimit: number,
        connectionRetryLimit: number,
        id: number,
        skipCheckLivelinessOnRelease: boolean,
        livelinessCheck: "query"|string
    };
    Parameter Description

    connectionConfig

    The configuration that will be used to create the connection in the pool
    This parameter is required.

    minAvailable

    Minimum number of connections that the pool will maintain
    The default value is 10.

    maxAge

    Time (in millisecond) from connection creation until it will age out
    The default value is 300000.

    checkTime

    The frequency at which the pool runs an internal liveliness check on free connections.
    The default value is 120000.
    Use 0 to disable liveliness check.

    maxLimit

    The maximum number of live connections the pool can maintain.
    The default value is 200.
    Use 0 to disable limit.

    connectionRetryLimit

    The maximum number of times a pool will attempt to create a connection.
    The default value is 5.

    id

    Assigns an id for the pool.
    The default value is new Date().getTime() .

    skipCheckLivelinessOnRelease

    Turns off liveliness checks on connections when they are released back to the pool.
    The default value is false, meaning a liveliness check will be performed when a connection is returned to the pool.

    livelinessCheck

    indicates the type of liveliness check to be performed.
    The default value is query, which means a query is used to test the connection.
    If set to any other than query, it will check if the NuoDB API isConnected returns true and a connection related exception is not trapped previously.

  2. Define a configuration object

    To connect to a NuoDB database provide a configuration object which includes the Connection Properties. Provide string values for each of the connection properties.

    With the configuration object defined, a connection to the desired NuoDB database can now be made.

  3. Initialize a connection pool

    Use the init() method to initialize a connection pool after the pool is created. This will populate the pool. The pool is now available for use. Once the pool has been successfully initialized it is ready for use. There are now free connections that can be requested for use.

    const poolConfig = {
        minAvailable: 10,
        connectionConfig,
        maxAge: 2000,
        checkTime: 10000,
        maxLimit: 12,
        connectionRetryLimit: 5,
    };
    
    async function() {
        const pool = new Pool(poolConfig);
        await pool.init()
    }
  4. Request connections from the connection pool

    Request connections using requestConnection(). You can request multiple connections. Each connection can then be used as a single connection.

    To see pool connections use pool.all_connections() and pool.free_connections().

    const conn1 = await Pool.requestConnection();
  5. Release connections back to the pool

    Once the user has finished using a connection, it must be returned to the pool. This allows the connection to be reused for subsequent requests. Release one single connection at a time. Manage each single connection’s lifecycle properly before requesting its release. Connections that have failed in any way should be returned to the pool where they will be dealt with and replaced.

    await pool.releaseConnection(conn1)
    Connections that have been released back to the connection pool should not be used anymore.
  6. Close the connection pool

    Close the connection pool using closePool() to release all resources.

    await pool.closePool()

    This step will close every connection within the pool, regardless of whether they are actively used.

    Users cannot manually close connections provided by the pool, and calling connection.close() on a connection provided by the pool will have the same effect as calling Pool.releaseConnection(connection).

Example

Example 1: Connect to NuoDB database using two connections on the connection pool and manage each connection as a single connection.
import { Driver } from "nuodb";

const connectionConfig = { //Configure connection pool
    database: `test`,
    password: "dba",
    user: "dba",
    port: "48004",
    schema: "USER"
};

const poolConfig = { // Define configuration object
    minAvailable: 10,
    connectionConfig,
    maxAge: 2000,
    checkTime: 10000,
    maxLimit: 12,
    connectionRetryLimit: 5,
};

async function() {
    let pool = null;

    try {
        pool = new Pool(poolConfig);
        const conn1 = await pool.requestConnection(); //Request connections from the connection pool
        const conn2 = await pool.requestConnection();

        await pool.releaseConnection(conn1); //Release connections back to the pool
        await pool.releaseConnection(conn2);

    } catch (err) {/* handle error */}

    finally {
        try {
            if (pool != null) {
                await pool.closePool() //Close the connection pool
            }
        } catch (err) {/* handle error */}
    }
}
The example uses try/catch semantics only. NuoDB Node.js driver supports try/catch, callback, and Promise semantics.