Script does not exit if MySQL connection is not closed

850 views
Skip to first unread message

Santacruz

unread,
Dec 26, 2011, 5:19:59 AM12/26/11
to nodejs
My code at https://github.com/santacruz123/node-bitcoin-arb

Using https://github.com/felixge/node-mysql for MySQL

I don't want to close/open connection before every function, but if I
don't do "db.end()" my script does not exit.

This is how I create object - https://github.com/santacruz123/node-bitcoin-arb/blob/master/mysql.js
- and declare it with " var db = require("./mysql"); "

What is right way to use it because I'm new to NodeJS.

Dave Clements

unread,
Dec 26, 2011, 8:36:55 PM12/26/11
to nodejs
You need to know when you want to exit your script, which is where you
call .end()

One point to note is that mysql.end is a graceful connection close, it
doesn't close until all queued SQL has been executed and the callbacks
of .query methods have completed, so you may find that putting
db.end() at the end of all your code does the job.

However, if there are other non-query related callbacks involved, that
your script needs to complete before you want the connection closed,
then it's a case of implementing some flow control

JoeZ99

unread,
Dec 27, 2011, 12:28:55 AM12/27/11
to nod...@googlegroups.com
Use a pool . I've used generic-pool with very good results.

Bradley Meck

unread,
Dec 27, 2011, 9:59:28 AM12/27/11
to nod...@googlegroups.com
If pooling is not an option (due to a long live biderectional comm channel such as logging and rpc) haibu-carapace unreferences the eventloop using a C++ plugin ( https://github.com/nodejitsu/haibu-carapace/blob/master/src/evref.cc ), however, this is an extreme edge case and should be avoided if possible.

Geerten van Meel

unread,
Dec 29, 2011, 7:05:54 PM12/29/11
to nodejs
One of the cool things about node is that you don't have to open and
close database connections with every http request. Instead you create
one single connection during startup and use it for all http requests.
When your node program shuts down, you close the connection as well.

Your program does not quit because the connection to the MySQL server
still does exist and node listens for data until you tell node to
close it. Then your program has nothing left to do and will exit.

On Dec 26, 11:19 am, Santacruz <vorand...@gmail.com> wrote:
> My code athttps://github.com/santacruz123/node-bitcoin-arb
>
> Usinghttps://github.com/felixge/node-mysqlfor MySQL
>
> I don't want to close/open connection before every function, but if I
> don't do "db.end()" my script does not exit.
>
> This is how I create object  -https://github.com/santacruz123/node-bitcoin-arb/blob/master/mysql.js
Reply all
Reply to author
Forward
0 new messages