Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting connection.release is not a function error in node

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;
like image 993
Ankur_009 Avatar asked Feb 18 '26 10:02

Ankur_009


1 Answers

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.

like image 56
Blake Simpson Avatar answered Feb 20 '26 01:02

Blake Simpson