最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getting connection.release is not a function error in node - Stack Overflow

programmeradmin1浏览0评论

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;

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;
Share Improve this question asked Dec 27, 2017 at 10:56 Ankur_009Ankur_009 3,7304 gold badges32 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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.

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

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

发布评论

评论列表(0)

  1. 暂无评论