36

Currently using: https://github.com/felixge/node-mysql

I have the following code:

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'me',
    password : 'secret',
    database : 'Database1'
});
app.put('/api/upload', function(req, res, next)
{
    connection.connect();
    doMultipleQueries(function(err)
    {
        connection.end();
    });          
};

The put request works perfectly fine, but calling it the second time, I get the following error

events.js:68
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: Cannot enqueue Handshake after invoking quit.
    at Protocol._validateEnqueue (/Users/anon/Desktop/project Web/node_modules/mysql/lib/protocol/Protocol.js:110:16)

Am I supposed to leave the connection open until the server dies?

UPDATE: When I move the mysql.createConnection into the put request function like so:

var connection = null; 
app.put('/api/upload', function(req, res, next)
{
    connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'me',
        password : 'secret',
        database : 'Database1'
    });
    connection.connect();
    doMultipleQueries(function(err)
    {
        connection.end();
    });          
};

It works fine. Does this mean connection.end() closes what mysql.createConnection created and cannot be reconnected?

2 Answers 2

37

connection.end() does it job regardless to fatal error. If a fatal error occurs before the COM_QUIT packet can be sent, an err argument will be provided to the callback, but the connection will be terminated regardless of that.

Also check destroy() method. This will cause an immediate termination of the underlying socket.

You can add error handler.

https://github.com/felixge/node-mysql/blob/master/Readme.md#error-handling

// I am Chuck Norris:
connection.on('error', function() {});

Once terminated, an existing connection object cannot be re-connected by design.

Check here. It's showing connecting back after disconnect.

https://github.com/felixge/node-mysql/blob/master/Readme.md#server-disconnects

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering, but it doesn't help me much. I actually did have code for connection.on error which displayed the error shown above. The end() properly closes the connection, but when I try to reconnect the connection, it doesn't connect.
I'm not a GitHub user so maybe you (someone) could easily tell those guys just to add a right curly brace at the end of the function passed in "connection.on". Thanks
4

I believe the proper way is to just get a connection for your app at startup, and end() it when your app closes.

7 Comments

Probably not efficient since one connection might stall during a complex query.
If you expect long queries, use a connection pool (that is the idea behind a pool, that there is already a connection, ready for use), or make sure your query is optimized properly. node-mysql can also let you deal with disconnects and other errors.
What do you mean by long queries ? More than 10 joins ?
@G4BB3R long query => long time consuming (for example taking >100ms)
No no no. This is correct using NoSQL (e.g. Mongo), but sql connections have a limit. If your limit is 15 (10 or 15 is the default), that means you can only have 15 people on your site at any one time. You should be closing the connections after you're done doing what you need to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.