I am working on a node.js application that connects to a MySQL server. This is probably not node.js-specific.
My code is currently initializing a MySQL connection when the application starts, and then it uses that connection every time it needs to make a request.
The problem that I am encountering with my approach is that the connection tends to close after a while. I'm not sure how long this period of time is, but it seems at least a few hours. I'm also not sure if this is due to inactivity.
In any case, I wonder what would be the best approach for managing MySQL connections in the long run. Of the possible approaches, I considered:
Just check before each request if the connection is still valid. If not, try again before executing the request.
Merge MySQL connections. Would this be redundant for a rather small application?
Periodically (every hour or so) run a query if this happens due to inactivity. However, this does not eliminate the situation in possible cases not caused by inaction.
Connect and disconnect before / after requests. Bad idea due to overhead.
I tend to use one of the first two options, as you can imagine. Which option would be the most reliable and effective?
source
share