Connection Pooling Best Practice
See original GitHub issueI’ve been using mssql with tedious and it’s been working well, but I’m concerned I’m not using the connection pool correctly. I’m using the built in sproc sp_who2 to see the connections from my applications. Even though I have only six instances of my app running with a max connections set to 20 I’m still seeing several connections in a sleeping status 800 + after running for a while.
I’m looking for a javascript way to ensure all connections get cleaned up even in cases of error. A best practice code snippet.
Thanks in advance,
var DAOConfig= {
    server: '*****',
    user:     '******',
    password: '********',
    driver: 'tedious',
    database: '******',
    options: {
        // tedious options
        //instanceName:''
        appName:'****',
        connectTimeout:15000,
        requestTimeout:150000
    },
    pool: {
        max: 20,
        min: 10,
        idleTimeoutMillis: 3000
    }
};
var sql = require('mssql');
var DAO = function(daoConfig, path) {
    var self = this;
    self.engineType = null;
    function doneCallBack(jobtype, elapsed){
        console.log("%s took %d seconds and (%d milliseconds or %d nanoseconds - pid:%d)", jobtype, elapsed[0], elapsed[1]/1000000, elapsed[1], process.pid);
    }
    self.Init = function (callback) {
        callback();
    };
    self.GetJobsFromQueue = function (sessionToken, engineID, callback) {
        var error = null;
        sql.connect(daoConfig, function (err) {
            if (err) {
                console.log(err);
            }
            else {
                var jobs = {
                    SessionToken: sessionToken,
                    Jobs:[]
                };
                var request = new sql.Request();
                request.input('Session', sql.VarChar(128), sessionToken);
                request.input('EngineID', sql.Int, engineID);
                //todo:make config value for MaxProcessingSeconds
                request.input('MaxProcessingSeconds', sql.Int, 300);
                request.input('BatchSize', sql.Int, 100);
                var start = process.hrtime();
                request.execute("[db].[schema].[sproc]", function (err, recordset) {
                    sql.close();
                    doneCallBack('Get jobs for session',process.hrtime(start));
                    if (err) {
                        console.log(err);                       
                        callback(err, null);
                    }
                    else {
                        if(recordset[0].length > 0) {
                          ...
                        }else{
                            console.log('No jobs found');
                        }
                        //build up jobs result
                            ...
                    }
                    callback(error, jobs);
                });
            }
        });
    };    
};
module.exports = DAO;
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:6
 Top Results From Across the Web
Top Results From Across the Web
Improve database performance with connection pooling
Open a connection to the database using the database driver. · Open a TCP socket for CRUD operations · Perform CRUD operations over...
Read more >Best-practices for tuning the JDBC Connection Pool
1. It is recommended that you enable the Validate Connection SQL by adding a validation SQL query. · 2. Always tune the Maximum...
Read more >A Simple Guide to Connection Pooling in Java - Baeldung
A quick overview of several popular connection pooling solutions, plus a quick dive into a custom connection pool implementation.
Read more >SQL Server Connection Pooling - ADO.NET - Microsoft Learn
Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection.
Read more >Choosing the right JDBC Connection Pool… | Javarevisited
What do you need from a good connection pooling framework? ... Although there are a lot of frameworks available to choose from like...
Read more > Top Related Medium Post
Top Related Medium Post
No results found
 Top Related StackOverflow Question
Top Related StackOverflow Question
No results found
 Troubleshoot Live Code
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free Top Related Reddit Thread
Top Related Reddit Thread
No results found
 Top Related Hackernoon Post
Top Related Hackernoon Post
No results found
 Top Related Tweet
Top Related Tweet
No results found
 Top Related Dev.to Post
Top Related Dev.to Post
No results found
 Top Related Hashnode Post
Top Related Hashnode Post
No results found

i hope it’s okay to post to this closed thread again. i’m facing a similar problem/question since no real best practice advise can be found on the net.
does that mean my database related functions (like getAccountsById() or updateUserSettings()) should look like your quick example. meaning that each function should create a new connection? what about closing the connection explicitly with “connection.close()” after the request was successful? is it needed or done automagically?
thanks!!
Closing due to inactivity.