{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/knex/issues/1198","repository_url":"https://api.github.com/repos/tgriesser/knex","labels_url":"https://api.github.com/repos/tgriesser/knex/issues/1198/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/knex/issues/1198/comments","events_url":"https://api.github.com/repos/tgriesser/knex/issues/1198/events","html_url":"https://github.com/tgriesser/knex/issues/1198","id":133325556,"number":1198,"title":"Not able to handle when server ends the connection","user":{"login":"woopstar","id":2997782,"avatar_url":"https://avatars.githubusercontent.com/u/2997782?v=3","gravatar_id":"","url":"https://api.github.com/users/woopstar","html_url":"https://github.com/woopstar","followers_url":"https://api.github.com/users/woopstar/followers","following_url":"https://api.github.com/users/woopstar/following{/other_user}","gists_url":"https://api.github.com/users/woopstar/gists{/gist_id}","starred_url":"https://api.github.com/users/woopstar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/woopstar/subscriptions","organizations_url":"https://api.github.com/users/woopstar/orgs","repos_url":"https://api.github.com/users/woopstar/repos","events_url":"https://api.github.com/users/woopstar/events{/privacy}","received_events_url":"https://api.github.com/users/woopstar/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tgriesser/knex/labels/PR%20please","name":"PR please","color":"bfe5bf"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":20,"created_at":"2016-02-12T19:38:40Z","updated_at":"2016-03-29T08:27:31Z","closed_at":null,"body":"I'm seeing this problem because there is a connection timeout for 600 seconds from the MySQL server. Then a connection is closed from the server instead of the client.\r\n\r\nWhen Knex then tried to execute a query, the following error comes:\r\n\r\n```\r\nUnhandled rejection Error: BEGIN; - This socket has been ended by the other party\r\n    at Socket.writeAfterFIN [as write] (net.js:297:12)\r\n    at Connection.write (/node_modules/mysql2/lib/connection.js:136:15)\r\n    at Connection.writePacket (/node_modules/mysql2/lib/connection.js:150:10)\r\n    at Query.start (/node_modules/mysql2/lib/commands/query.js:39:14)\r\n    at Query.Command.execute (/node_modules/mysql2/lib/commands/command.js:34:20)\r\n    at Connection.handlePacket (/node_modules/mysql2/lib/connection.js:310:28)\r\n    at Connection.addCommand (/node_modules/mysql2/lib/connection.js:326:10)\r\n    at Connection.query (/node_modules/mysql2/lib/connection.js:397:15)\r\n    at /node_modules/knex/lib/dialects/mysql/index.js:92:18\r\n    at tryCatcher (/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)\r\n    at Promise._resolveFromResolver (/node_modules/knex/node_modules/bluebird/js/main/promise.js:480:31)\r\n    at new Promise (/node_modules/knex/node_modules/bluebird/js/main/promise.js:70:37)\r\n    at Client._query (/node_modules/knex/lib/dialects/mysql/index.js:88:12)\r\n    at Client.query (/node_modules/knex/lib/client.js:127:24)\r\n    at /node_modules/knex/lib/transaction.js:219:21\r\n    at tryCatcher (/node_modules/knex/node_modules/bluebird/js/main/util.js:26:23)\r\n```"},"comment":{"url":"https://api.github.com/repos/tgriesser/knex/issues/comments/202776030","html_url":"https://github.com/tgriesser/knex/issues/1198#issuecomment-202776030","issue_url":"https://api.github.com/repos/tgriesser/knex/issues/1198","id":202776030,"user":{"login":"woopstar","id":2997782,"avatar_url":"https://avatars.githubusercontent.com/u/2997782?v=3","gravatar_id":"","url":"https://api.github.com/users/woopstar","html_url":"https://github.com/woopstar","followers_url":"https://api.github.com/users/woopstar/followers","following_url":"https://api.github.com/users/woopstar/following{/other_user}","gists_url":"https://api.github.com/users/woopstar/gists{/gist_id}","starred_url":"https://api.github.com/users/woopstar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/woopstar/subscriptions","organizations_url":"https://api.github.com/users/woopstar/orgs","repos_url":"https://api.github.com/users/woopstar/repos","events_url":"https://api.github.com/users/woopstar/events{/privacy}","received_events_url":"https://api.github.com/users/woopstar/received_events","type":"User","site_admin":false},"created_at":"2016-03-29T08:27:31Z","updated_at":"2016-03-29T08:27:31Z","body":"Sorry for late reply.\r\n\r\nWe use transactions for all queries that alter data on the server - so yes, we are using transactions and this is causing the problem.\r\n\r\nThe problem does not rely on that the transaction takes 600 seconds, but that the connection from the server is closed after 600 seconds. When the transaction is starting, it tries to use a pool object that is actually closed and this causes the issue.\r\nThe only solution we found yet, is to query ever 500 second to keep the connection open and used.\r\n\r\nWhen a transaction or a query starts / is fired, the pool object must be validated and checked if it's actually still connected. It thinks it is, because it dosen't see that the server closed the connection.\r\n\r\nMakes sense?"}}