question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection Pooling Best Practice

See original GitHub issue

I’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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

5reactions
steffenstolzecommented, Feb 19, 2016

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.

From the code you posted I can see you’re using global connection. I would recomend you to use new connection instead. See: https://github.com/patriksimek/node-mssql#quick-example

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!!

0reactions
patriksimekcommented, Oct 27, 2014

Closing due to inactivity.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found