NodeJS [SOLVED]: getting connection.release is not a function error in node

NodeJS [SOLVED]: getting connection.release is not a function error in node

Home Forums Scripting NodeJS Tutorials NodeJS [SOLVED]: getting connection.release is not a function error in node

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245806

    Cloudy Point
    Keymaster

    QuestionQuestion

    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;
    

    #245807

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    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.

    Source: https://stackoverflow.com/questions/47990939/getting-connection-release-is-not-a-function-error-in-node
    Author: Blake Simpson
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.