Unexpected behavior in asynchronous javascript

2

I am writing a backend code using javascript, node and the npm modules 'mysql' and 'request'. I tried to write a module to do a pooling of SQL database connections by different API calls in various files of my project. My module mysqlLib.js:

var mysql = require('mysql');

var pool = mysql.createPool({
    host : settings.host_DB,
    user : settings.user_DB,
    password : settings.senha_DB,
    database : settings.DB
});

module.exports.getConnection = function(callback) {
  pool.getConnection(function(err, conn) {
    if(err) {
      return callback(err);
    }
    callback(err, conn);
    conn.release();
  });
};

At the end of this module I decided to test with a tester.js file

var mysql = require('./lib/mysqlLib.js');
var request = require('request');

mysql.getConnection(function(err, conn) {
    conn.query(query1, function(err) {
        conn.query(query2, function(err) {
            request.get(url1, function(err, httpResponse, body){
                conn.query(query3);
            });     
        });
    });
});

In fact, the code did what I wanted it to do: Every time I need to make changes to my database through some middleware in the API that I wrote in any of my files, use the getConnection method of mysqlLib. js to pull a connection from my pool and at the end of use release it back to the pool. What's bothering me is that I noticed something that should be taking this little program to an error and it's not ... See, the'conn.query (query, callback) 'function is asynchronous, that means it does not interrupt the code flow. Thus, I thought that by performing the first invocation of 'conn.query' in tester.js, this function would be executed asynchronously allowing the getConnection callback to come to an end. Should not this mean that the program would terminate the callback and release () the connection that was pulled? I thought I could call the first conn.query but once the callback came to its end and the connection was released I would not be able to perform the other queries nested at first because of the lack of it but they are all being executed without error! I wonder what may possibly be happening, why is the connection only released after all callbacks nested in the getConnection callback?

    
asked by anonymous 31.03.2017 / 04:48

1 answer

1

This library allows you to configure the maximum number of connections when you are using pool . If you do not specify a value max is 10 as documented :

  

connectionLimit : The maximum number of connections to create at once. ( Default : 10)

In addition, although the code is asynchronous, the browser reads functions synchronously. That is, when you read the first conn.query(... , it opens a connection and only terminates it when the function returns. Not the asynchronous return via callback, but when I have read all the code inside the function. Before that happens, mysql will not close the connection unless there is a specific .end() in the code.

So, before the first function gives return it opens the nested ones, so the last to be called is the first to be closed.

    
31.03.2017 / 06:50