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.

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

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.