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

JN_newbie
  • 5,492
  • 14
  • 59
  • 97

2 Answers2

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();
});
Enrique Fueyo
  • 3,358
  • 1
  • 16
  • 10
  • Thanks. Is this is a better approach to handle the connection? – JN_newbie Mar 10 '16 at 11:32
  • One question. In this way are we sharing a single connection? – JN_newbie Mar 10 '16 at 12:04
  • @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 – Enrique Fueyo Mar 10 '16 at 18:30
  • 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? – JN_newbie Mar 11 '16 at 05:38
  • I guess it depends on your use case, DB size, queries you do, number of connections... – Enrique Fueyo Mar 11 '16 at 10:34
2

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();
    }) 
});
PostCrafter
  • 655
  • 5
  • 15