Best practices for connect() and end()

296 views
Skip to first unread message

Charles Stanton

unread,
Mar 28, 2015, 2:52:09 PM3/28/15
to node-...@googlegroups.com
I love the module but am slightly confused on the best practice for initiating and ending a connection. On one hand it seems wasteful of resources to call connection.connect() before each query because of the time, etc. required to establish a connection.  On the other hand one cannot depend on starting a connection early in the app and being able to use it forever because of timeouts, server shut down, etc.  And a related issue is the best way to determine if a connection is available (not pooling).  A method connection.isConnected() is not available so should we just ping?

If we connect() before each query (a questionable practice as stated above) then should we immediately end() in the query callback?  What happens if we never call connection.end()?  For small usage I imagine that the MySQL server will kill the connections and throw them overboard but for a large application with millions of requests I can see that dangling connections could be a problem.  Considering the asynchronous nature of node.js the connect() and end() placement could be critical.

Suggested best practices?

Patrick Motard

unread,
Jan 26, 2016, 10:00:42 AM1/26/16
to node-mysql
Charles,
Have you found an answer to this? 

Charles Hickman

unread,
Jul 12, 2017, 12:07:24 PM7/12/17
to node-mysql
I agree with your concerns. I'm using a connection-last-used time variable which assumes that when its not zero that there is a valid connection and can bypass connect(). The connection date is updated every time the mySql database is queried and at the same time triggers a timeout function which periodically checks the connection date to see how long ago the database was last used. When it reaches longer than 60 seconds, connection.end() is called.

Ryan Lee

unread,
Jul 12, 2017, 3:52:45 PM7/12/17
to node-...@googlegroups.com
Wait, doesn't a pool take care of this for you? Additionally, if you only want 1 connection, can you set the pool size to be 1?

--Ryan

--
You received this message because you are subscribed to the Google Groups "node-mysql" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+unsubscribe@googlegroups.com.
To post to this group, send email to node-...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-mysql.
For more options, visit https://groups.google.com/d/optout.

Charles Hickman

unread,
Jul 13, 2017, 5:59:07 AM7/13/17
to node-mysql, ryansa...@gmail.com
Hi Ryan, I've just started using this package and had initially glossed over the pool object because I only ever need one connection at a time (single user). I've just taken a look at the documentation and can see that using pool will take care of testing for, using or creating an existing connection, and I see you can use pool.release() to release a connection back to the pool; but you still need to call pool.end() to terminate all pooled connections, otherwise a connection, once created, will remain active until the node app ends or mySql terminates it or is itself terminated (both of which generates a fatal error in my node app, stopping the web server).
To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+...@googlegroups.com.

Ryan Lee

unread,
Jul 13, 2017, 10:55:57 AM7/13/17
to node-...@googlegroups.com
What's your use case? Are you having problems with terminations? Why do you want to close the connection?

Your other option is to just use connection.destory() after you finish with the pooled connection. The next request to the pool will create a new connection.

--Ryan

To unsubscribe from this group and stop receiving emails from it, send an email to node-mysql+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages