{"action":"closed","issue":{"url":"https://api.github.com/repos/felixge/node-mysql/issues/1061","labels_url":"https://api.github.com/repos/felixge/node-mysql/issues/1061/labels{/name}","comments_url":"https://api.github.com/repos/felixge/node-mysql/issues/1061/comments","events_url":"https://api.github.com/repos/felixge/node-mysql/issues/1061/events","html_url":"https://github.com/felixge/node-mysql/issues/1061","id":70177922,"number":1061,"title":"Suggestions on gracefully recovering from a DB crash/restart with poolCluster","user":{"login":"adrianblynch","id":4160967,"avatar_url":"https://avatars.githubusercontent.com/u/4160967?v=3","gravatar_id":"","url":"https://api.github.com/users/adrianblynch","html_url":"https://github.com/adrianblynch","followers_url":"https://api.github.com/users/adrianblynch/followers","following_url":"https://api.github.com/users/adrianblynch/following{/other_user}","gists_url":"https://api.github.com/users/adrianblynch/gists{/gist_id}","starred_url":"https://api.github.com/users/adrianblynch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adrianblynch/subscriptions","organizations_url":"https://api.github.com/users/adrianblynch/orgs","repos_url":"https://api.github.com/users/adrianblynch/repos","events_url":"https://api.github.com/users/adrianblynch/events{/privacy}","received_events_url":"https://api.github.com/users/adrianblynch/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/felixge/node-mysql/labels/question","name":"question","color":"d4c5f9"}],"state":"closed","locked":false,"assignee":{"login":"dougwilson","id":67512,"avatar_url":"https://avatars.githubusercontent.com/u/67512?v=3","gravatar_id":"","url":"https://api.github.com/users/dougwilson","html_url":"https://github.com/dougwilson","followers_url":"https://api.github.com/users/dougwilson/followers","following_url":"https://api.github.com/users/dougwilson/following{/other_user}","gists_url":"https://api.github.com/users/dougwilson/gists{/gist_id}","starred_url":"https://api.github.com/users/dougwilson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dougwilson/subscriptions","organizations_url":"https://api.github.com/users/dougwilson/orgs","repos_url":"https://api.github.com/users/dougwilson/repos","events_url":"https://api.github.com/users/dougwilson/events{/privacy}","received_events_url":"https://api.github.com/users/dougwilson/received_events","type":"User","site_admin":false},"milestone":null,"comments":3,"created_at":"2015-04-22T16:46:19Z","updated_at":"2015-06-17T05:52:31Z","closed_at":"2015-06-17T05:52:31Z","body":"We're using the following `poolCluster.getConnection('*', 'RANDOM', function (err, connection) { ... }`. If a request comes into our API when the DB is down we get the `POOL_NOEXIST` error as expected.\r\n\r\nOnce the DB is back up, the pool of connections can't be used.\r\n\r\n*My question is, what are people doing to gracefully get things back up and running again?*\r\n\r\nA bit more detail about what we have in case it's important to the answer...\r\n\r\n```\r\nvar config = require('./config')();\r\nvar log = require('./log');\r\nvar poolCluster = mysql.createPoolCluster();\r\n\r\nconfig.database.main.forEach(function (serverConfig) {\r\n\tpoolCluster.add(serverConfig.id, serverConfig);\r\n});\r\n\r\npoolCluster.on('remove', function (nodeId) {\r\n\tlog.error('Removed mysql node: ' + nodeId);\r\n\t// TODO: Look for another way as this uses a private method\r\n\tif (poolCluster._findNodeIds().length === 0) {\r\n\t\tvar errorMsg = 'No remaining mysql servers, exiting';\r\n\t\tlog.error(errorMsg);\r\n\t\tthrow new Error(errorMsg);\r\n\t}\r\n});\r\n\r\nexports.conn = {\r\n\tquery: function (sql, values, cb) {\r\n\t\tvar query = mysql.createQuery(sql, values, cb);\r\n\r\n\t\tpoolCluster.getConnection('*', 'RANDOM', function (err, connection) {\r\n\t\t\tif (err) {\r\n\t\t\t\tquery.on('error', function () {\r\n\t\t\t\t\tlog.error('sql error', err);\r\n\t\t\t\t});\r\n\t\t\t\tquery.end(err);\r\n\t\t\t\treturn;\r\n\t\t\t}\r\n\r\n\t\t\tquery.once('end', function () {\r\n\t\t\t\tconnection.release();\r\n\t\t\t});\r\n\r\n\t\t\tconnection.query(query);\r\n\t\t});\r\n\r\n\t\treturn query;\r\n\t}\r\n};\r\n```"}}