Home > OS >  SQL Server and NodeJS - Connection Pool availabilty is always zero
SQL Server and NodeJS - Connection Pool availabilty is always zero

Time:10-05

I'm trying to initialize a pool of SQL Server connections for my nodejs web application to use. I've set the config to create 10 min connections but when I start the app with the below code. I only have 0 available connections which doesn't allow me to begin any transactions.

Any help would be greatly appreciated!

test.js

const sql = require('mssql');
require('dotenv').config();

const appPool = new sql.ConnectionPool({
    user: process.env.DB_USER,
    password: process.env.DB_PWD,
    database: process.env.DB_NAME,
    server: process.env.DB_HOST,
    pool: {
        min: 10,
        max: 100,
        acquireTimeoutMillis: 15000,
    },
    options: {
        encrypt: true,
        trustServerCertificate: false
    }
});

appPool.connect().then(pool => {
    console.log(`SERVER: Connected to the db and ${pool.available} connections are available!`);
});

Output

MINGW64 ~/Desktop/React Projects/dummy-project (master)
$ node test.js
SERVER: Connected to the db and 0 connections are available!

CodePudding user response:

node-mssql uses tarn.js for connection pooling. The connections are not created preemptively. Instead, they're created as and when the connections are requested. Once the number of concurrent requests for connections exceed pool.min, the available connections in the pool stays at that level.

So, doing a transaction or running a query shouldn't be a problem, node-mssql will create the connection and fulfil the request.

If you really want to have pool.min connections in your pool, you can write a small script to fire some concurrent queries on your database which will warm up the pool for you.

  • Related