3

I am trying to setup connection pool and wants to release the connection once done with query.

How I am implementing:

dbConnection.getConnection( function(err, connection){

                dbConnection.release();

    if (err) {
      console.log("db error ", err);
      return callback(err);
      //connection.release();
    }else {

      var memberId= member.member_id;

      return dbConnection.query("Select * from tableName, function(err, result,fields){



        if (err) {
          return callback(err);
        }else {


          return callback(null, result);
        }

      });


    }

    dbConnection.on('error', function(err) {
           console.log("db on error ");
           callback(err);
           return;
    });

I am getting the error:

TypeError: dbConnection.release is not a function
    at Query._callback (/home/itstym/node_js/perb/models/member.js:26:22)
    at Query.Sequence.end (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
    at Query._handleFinalResultPacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
    at Query.EofPacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/sequences/Query.js:123:8)
    at Protocol._parsePacket (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/home/itstym/node_js/perb/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/home/itstym/node_js/perb/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)

But I have gone through numerous example available on internet, all are doing the same thing. Do I am missing something?

dbconnection.js

var dbconnection = mysql.createPool({

  connectionLimit : 10, //important
  host:'localhost',
  user:'root',
  database:'perb',
  password: 'dbandc',
  debug    :  false
});


module.exports=dbconnection;

2 Answers 2

4

You want to call release() on the connection variable passed from the callback, not on the pool itself. The first 2 lines should instead be:

dbConnection.getConnection( function(err, connection){

  connection.release();

I basically just changed dbConnection to connection on line 3.

Sign up to request clarification or add additional context in comments.

Comments

0

Please use connection.release() instead of dbConnection.release();

please refer https://www.npmjs.com/package/mysql#pooling-connections

1 Comment

Thanks a lot. @aToz

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.