Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 98378  / 12 Years ago, wed, november 7, 2012, 12:00:00

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?


More From » mysql

 Answers
6

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


[#82136] Tuesday, November 6, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neildrews

Total Points: 166
Total Questions: 103
Total Answers: 85

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
neildrews questions
Fri, Feb 18, 22, 00:00, 2 Years ago
Tue, Oct 12, 21, 00:00, 3 Years ago
Tue, Mar 23, 21, 00:00, 3 Years ago
Sun, Aug 16, 20, 00:00, 4 Years ago
;