2

I see very few online posts when it comes to NodeJS with IBM DB2. I am new to NodeJS and having issues to configure connection pooling for my web app. I am successfully running node app with single connection in my local but not sure how to configure connection pooling. Below code is how I have it for single connection.

DBConnection JS:

module.exports = function(dbConnection)
{
 var Pool = require('ibm_db').Pool;
 var pool = new Pool();

 pool.open("MY_CONNECTION_STRING",function(err,connection){

  //error handling logic ...

  dbConnection(connection);
 });
}

App listener js:

var express = require('express');
var app = express();

app.listen(8080,function(){
 console.log("server started..);
});

require('./DBConnection')(function(connection){

  app.get('/getEmpId',function(req,res){
   connection.query("MY_SQL_QUERY",function(error,results,fields){
    //some other logic

    res.send(results[0]);   

    //Closing connection     
    connection.close(function(err2) {
        if(err2) console.log(err2);
    });
   });
  });      
}

Need your suggestions to setup connection pool where I can use one connection for each request when concurrent users are accessing and close the connection after serving request.

1 Answer 1

3

You can take a look at the brief samples provided with the IBM node-ibm_db driver. It has a section on Connection Pooling. The driver reuses the node-odbc pool and you need to invoke the open/close calls on the Pool object. Here is the sample taken from the node-ibm_db site:

var Pool = require("ibm_db").Pool
    , pool = new Pool()
    , cn = "DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx";

pool.open(cn, function (err, db) {
    if (err) {
        return console.log(err);
    }

    //db is now an open database connection and can be used like normal
    //if we run some queries with db.query(...) and then call db.close();
    //a connection to `cn` will be re-opened silently behind the scense
    //and will be ready the next time we do `pool.open(cn)`
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank You. A followup question, How do I set pool size?
There is an API for that: github.com/ibmdb/node-ibm_db/blob/master/… BTW: Mark the question answered.
@data_henrik is pooling suitable for only select queries ? what about insert and how can return the data at the end ?
Isn't that a question on its own?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.