{"action":"created","issue":{"url":"https://api.github.com/repos/felixge/node-mysql/issues/532","repository_url":"https://api.github.com/repos/felixge/node-mysql","labels_url":"https://api.github.com/repos/felixge/node-mysql/issues/532/labels{/name}","comments_url":"https://api.github.com/repos/felixge/node-mysql/issues/532/comments","events_url":"https://api.github.com/repos/felixge/node-mysql/issues/532/events","html_url":"https://github.com/felixge/node-mysql/issues/532","id":16353603,"number":532,"title":"Connection lost. The server closed the connection","user":{"login":"sapkal-manish","id":1754555,"avatar_url":"https://avatars.githubusercontent.com/u/1754555?v=3","gravatar_id":"","url":"https://api.github.com/users/sapkal-manish","html_url":"https://github.com/sapkal-manish","followers_url":"https://api.github.com/users/sapkal-manish/followers","following_url":"https://api.github.com/users/sapkal-manish/following{/other_user}","gists_url":"https://api.github.com/users/sapkal-manish/gists{/gist_id}","starred_url":"https://api.github.com/users/sapkal-manish/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sapkal-manish/subscriptions","organizations_url":"https://api.github.com/users/sapkal-manish/orgs","repos_url":"https://api.github.com/users/sapkal-manish/repos","events_url":"https://api.github.com/users/sapkal-manish/events{/privacy}","received_events_url":"https://api.github.com/users/sapkal-manish/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2013-07-04T06:29:33Z","updated_at":"2016-04-30T07:12:17Z","closed_at":"2014-04-27T22:50:13Z","body":"I am using mysql@2.0.0-alpha7 with node.js.\r\n\r\nI have written code as follows: \r\n\r\n```javascript\r\nvar mysql = require('mysql');\r\nvar pool  = mysql.createPool({\r\n  host     : 'example.org',\r\n  user     : 'bob',\r\n  password : 'secret',\r\n});\r\n\r\npool.getConnection(function(err, connection) {\r\n   connection.query(query, function (err, rows, fields) {\r\n        connection.end();\r\n                      \r\n        if (err) {\r\n       \t   //Return Error\r\n        };\r\n      \t//Return result\r\n   });\r\n});\r\n```\r\n\r\nBut Now a days, I am getting error \"Connection lost. The server closed the connection.\"  \r\nDetailed error is like this:\r\n```\r\n Thu Jul 04 2013 00:40:47 GMT+0000 (Coordinated Universal Time) - uncaughtException : Error: Connection lost: The server closed the connection. stack Error: Connection lost: The server closed the connection.\r\n    at Protocol.end (C:\\inetpub\\wwwroot\\NodeJs\\node_modules\\mysql\\lib\\protocol\\Protocol.js:72:13)\r\n    at Socket.onend (stream.js:66:10)\r\n    at Socket.EventEmitter.emit (events.js:126:20)\r\n    at TCP.onread (net.js:418:51) \r\n```\r\nWhat is the solution for the problem.\r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/felixge/node-mysql/issues/comments/215943340","html_url":"https://github.com/felixge/node-mysql/issues/532#issuecomment-215943340","issue_url":"https://api.github.com/repos/felixge/node-mysql/issues/532","id":215943340,"user":{"login":"swapnilaryan","id":12202963,"avatar_url":"https://avatars.githubusercontent.com/u/12202963?v=3","gravatar_id":"","url":"https://api.github.com/users/swapnilaryan","html_url":"https://github.com/swapnilaryan","followers_url":"https://api.github.com/users/swapnilaryan/followers","following_url":"https://api.github.com/users/swapnilaryan/following{/other_user}","gists_url":"https://api.github.com/users/swapnilaryan/gists{/gist_id}","starred_url":"https://api.github.com/users/swapnilaryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swapnilaryan/subscriptions","organizations_url":"https://api.github.com/users/swapnilaryan/orgs","repos_url":"https://api.github.com/users/swapnilaryan/repos","events_url":"https://api.github.com/users/swapnilaryan/events{/privacy}","received_events_url":"https://api.github.com/users/swapnilaryan/received_events","type":"User","site_admin":false},"created_at":"2016-04-30T07:12:17Z","updated_at":"2016-04-30T07:12:17Z","body":"you can use this code \r\n`var db_config = {\r\n  host: 'localhost',\r\n    user: 'root',\r\n    password: '',\r\n    database: 'example'\r\n};\r\n\r\nvar connection;\r\n\r\nfunction handleDisconnect() {\r\n  connection = mysql.createConnection(db_config); // Recreate the connection, since\r\n                                                  // the old one cannot be reused.\r\n\r\n  connection.connect(function(err) {              // The server is either down\r\n    if(err) {                                     // or restarting (takes a while sometimes).\r\n      console.log('error when connecting to db:', err);\r\n      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,\r\n    }                                     // to avoid a hot loop, and to allow our node script to\r\n  });                                     // process asynchronous requests in the meantime.\r\n                                          // If you're also serving http, display a 503 error.\r\n  connection.on('error', function(err) {\r\n    console.log('db error', err);\r\n    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually\r\n      handleDisconnect();                         // lost due to either server restart, or a\r\n    } else {                                      // connnection idle timeout (the wait_timeout\r\n      throw err;                                  // server variable configures this)\r\n    }\r\n  });\r\n}\r\n\r\nhandleDisconnect();`"}}