bminer / node-mysql-queues

Wraps 'node-mysql' to provide mulitple query queues, allowing support for multiple statements and transactions.
MIT License
92 stars 11 forks source link

committing the transaction causes application to hang for ever #8

Closed Hubro closed 12 years ago

Hubro commented 12 years ago

This is my code

var con = mysql.createClient({
    user: m.username,
    password: m.password,
    database: m.database
});
queues(con, true);
var trans = con.startTransaction();

trans.query('UPDATE Streams SET live=0;');
trans.commit();
trans.execute();

The application runs like it should, the transaction succeeded as far as MySQL is concerned, but the application never exits. If I remove commit, the commit is implied and I get a warning, and the application still hangs for ever. If I remove both commit and execute, the program runs fine and exits like it should (But the SQL is never executed, obviously). Removing just execute has no effect, even though one would think that the transaction queue wouldn't ever start in that case.

I've tried debugging the issue using node-inspector. When I pause the application during the post-execution eternal hang, the call stack just says

(program) (internal script)

Have I done something wrong, or is this indeed a bug?

node-mysql version: 0.9.5 node-mysql-queues version: 0.3.3

bminer commented 12 years ago

I do not think this is a bug. Node will only terminate itself when there is "nothing left to do." Since there is a open Socket connection (your connection stream to the MySQL server is still open), Node seems to "hang."

You should consider calling con.end() like this:

//...
trans.query('UPDATE Streams SET live=0;');
trans.commit(function() {
    con.end();
});

This will work also (in this case), but is not recommended:

//...
trans.query('UPDATE Streams SET live=0;');
trans.commit();
con.end();

In this case, the 'END' query is queued for execution immediately after your transaction, and if you had nested queries or something more complex like that, 'END' may be executed beforehand, killing your connection too soon. So, it's best to call end() inside of your last query's callback function.

Please let me know if that helps. Thanks!

Hubro commented 12 years ago

I had no idea the MySQL connection would keep node open. I've never had a problem with this before using mysql-queues though. Perhaps you should give people a heads-up in the documentation that this could happen. In any case, thanks a lot for the quick help! :-)

bminer commented 12 years ago

I agree -- I fell into this trap when I was using node-mysql, too. I think this is definitely more of a node-mysql "issue," so I'm gunna leave it up to those guys to include the heads-up in their documentation. ;)