Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does connectionLimit work in node mysql?

I quite don't understand how connectionLimit in mysql module works.

I have created a connectionPool in following way:

  var connPool = mysql.createPool({
        host: config.get('database.host'),
        user: config.get('database.user'),
        password: config.get('database.password'),
        database: config.get('database.dbname'),
        connectionLimit: 5
  });

Now, what does limit 5 do here?

Here's what I think:

Suppose there are 5 users using my REST API for selecting some data from a table. Now in my API, I get a connection from pool, do stuff and then release the connection.

So now, suppose there are 12 users who send request to this API concurrently. So first 5 of them should get access to the data and other 7 will get error / no response. They will have to request again to get the data.

Is this like so or something else?

like image 999
Vikas Avatar asked Aug 05 '17 14:08

Vikas


People also ask

Can NodeJs communicate with MySQL?

Here we are calling connect function on the connection variable which we have created already. Now we will see the following output on the terminal as shown in the screenshot: In this way, NodeJs application can be connected with the Mysql database.

How many simultaneous connections can MySQL handle?

Each database user is limited to 38 simultaneous MySQL connections. This limitation helps to prevent overloading the MySQL server to the detriment of other sites hosted on the server.


1 Answers

Is this like so or something else?

It's something else.

If connectionLimit is 10, the maximum number of connections that the driver will make to the database is 10.

Assuming the default driver options, if all of those connections are active (that is, they have been acquired from the pool to perform a query, but they haven't yet been released), then the next request for a connection (pool.getConnection()) will be queued: the driver will wait until one of the active connections gets released back into the pool. Your user will only notice a bit of a delay, but they won't get an error or no response; just a delayed response.

All this can be fine-tuned using the options documented here. For instance, if you prefer that instead of waiting, the driver returns an error, set waitForConnections : false.

Or, if you want the driver to return an error if, say, there are 25 requests queued to receive a connection, set queueLimit : 25.

like image 59
robertklep Avatar answered Oct 13 '22 06:10

robertklep