I do not remember my initial use of this mechanism. Currently, I cannot come up with a single valid use case.
Your client should be able to determine when the connection is lost, and allow you to recreate the connection. If it is important that part of the program logic is executed using the same connection, use transactions.
TL; dr; Do not use this method.
A pragmatic solution is to get MySQL to maintain the connection:
setInterval(function () { db.query('SELECT 1'); }, 5000);
I prefer this solution to connection pooling and disconnect processing because it does not require structuring your code so that it is aware of the connection. Executing the request every 5 seconds ensures that the connection remains active and PROTOCOL_CONNECTION_LOST does not happen.
In addition, this method ensures that you support the same connection , not reconnection. It is important. Think about what happens if your script uses LAST_INSERT_ID() and the mysql connection was dropped without your knowledge?
However, this only ensures that the connection wait_timeout ( wait_timeout and interactive_timeout ) does not occur. This will fail, as expected, in all other scenarios. Therefore, be sure to handle other errors.
Gajus Jan 29 '15 at 13:14 2015-01-29 13:14
source share