0

my node.js file is interacting with a MySQL database through the MySQL module. For now I basically just create a pool

var connection = mysql.createPool(dataConnection);

then I make simple queries using connection.query().

Sometimes I get an error saying Error: Connection lost: The server closed the connection. From what I've understood, whenever I call the query method a brand new connection is created from the pool, then it is closed immediately after it is done. Hence I am quite puzzled: how can the connection be closed since the server should explicitly create one for this query? And, most importantly, is there a way I can actually avoid this issue (which doesn't happen too often, fortunately, but still).

Thanks for your help!

Noël.

Noël Nadal
  • 35
  • 1
  • 7

1 Answers1

2

Since you haven't shared a lot of details and code related to connection pool creation, I'll try to ans as best as I can.

    var pool = mysql.createPool(dataConnection);
    // check if you have configured any timeouts etc for the pool

I'm not sure about the exact code, but it should look something like this:

connection = pool.GetConnection();
result = connection.query();
// now instead of closing the connection, it should be returned/released back to the pool
connection.release();

How do I create a MySQL connection pool while working with NodeJS and Express?

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42