5

I am unable to export a pool connection in node.js. What I want is to get the connection from the pool from the db.js and use it and then release it after using it.

db.js

var mySQL = require('mysql');
    var pool  = mySQL.createPool({
        host: config.host,
        user: config.user,
        password: config.password,
        database: config.database
    });
    var getConnection = function() {
        pool.getConnection(function(err, connection) {
            if(err) throw err;
            return connection;
        });
    };
    module.exports.pool = getConnection;

query.js

var dbSql = require('./db');
var userQuery = 'select * from user';
var con = dbSql.pool();
console.log("con: " + con); //displays undefined
con.query(userQuery,function(err,user){
   con.release();
})

above when I do console.log("con: " + con); it displays undefined

2 Answers 2

7

You are exporting a function, not the pool itself. Besides, getConnection is not accepting any callback:

db.js should be something like this:

var mySQL = require('mysql');
var pool  = mySQL.createPool({
    host: config.host,
    user: config.user,
    password: config.password,
    database: config.database
});
var getConnection = function (cb) {
    pool.getConnection(function (err, connection) {
        //if(err) throw err;
        //pass the error to the cb instead of throwing it
        if(err) {
          return cb(err);
        }
        cb(null, connection);
    });
};
module.exports = getConnection;

query.js should be something like this:

var getConnection = require('./db');
getConnection(function (err, con) {
  if(err) { /* handle your error here */ }
  var userQuery = 'select * from user';
  console.log("con: " + con); //displays undefined
  con.query(userQuery,function(err,user){
  con.release();
});
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Is this is a better approach to handle the connection?
One question. In this way are we sharing a single connection?
@Java_NewBie they arent the same connection, unless pool.getConnection returns always the same connection. db.js doesn't export a connection but a function to get a connection
Thankyou. It is good to use the different connections or only the one connection? I think different connection are also good enough because at the end we are also releasing that used connection so that connection can be resused. right?
I guess it depends on your use case, DB size, queries you do, number of connections...
3

The problem you have is your understanding of how callbacks and asychronous calls work in JavaScript and Node.js.

To understand the concept check this article

You will have to change your code to something like this:

db.js

var mySQL = require('mysql');
var pool  = mySQL.createPool({
    host: config.host,
    user: config.user,
    password: config.password,
    database: config.database
});
module.exports.pool = pool.getConnection; // export the pools getConnection

query.js

var dbSql = require('./db');
var userQuery = 'select * from user';
dbSql.pool(function(err, con) { // the function is called when you have a connection
    if(err) throw err; // or handle it differently than throwing it
    console.log("con: " + con); // not undefined anymore
    con.query(userQuery,function(err,user){
        con.release();
    }) 
});

Comments

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.