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:
-
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 is10
.maxAge
Time (in millisecond) from connection creation until it will age out
The default value is300000
.checkTime
The frequency at which the pool runs an internal liveliness check on free connections.
The default value is120000
.
Use0
to disable liveliness check.maxLimit
The maximum number of live connections the pool can maintain.
The default value is200
.
Use0
to disable limit.connectionRetryLimit
The maximum number of times a pool will attempt to create a connection.
The default value is5
.id
Assigns an id for the pool.
The default value isnew Date().getTime()
.skipCheckLivelinessOnRelease
Turns off liveliness checks on connections when they are released back to the pool.
The default value isfalse
, 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 isquery
, which means a query is used to test the connection.
If set to any other thanquery
, it will check if the NuoDB APIisConnected
returns true and a connection related exception is not trapped previously. -
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.
-
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() }
-
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()
andpool.free_connections()
.const conn1 = await Pool.requestConnection();
-
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. -
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 callingPool.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 supportstry/catch
,callback
, andPromise
semantics.