{"action":"created","issue":{"url":"https://api.github.com/repos/epochtalk/core-pg/issues/21","labels_url":"https://api.github.com/repos/epochtalk/core-pg/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/epochtalk/core-pg/issues/21/comments","events_url":"https://api.github.com/repos/epochtalk/core-pg/issues/21/events","html_url":"https://github.com/epochtalk/core-pg/issues/21","id":102075600,"number":21,"title":"createTransaction always removes connection from pool","user":{"login":"danneu","id":529580,"avatar_url":"https://avatars.githubusercontent.com/u/529580?v=3","gravatar_id":"","url":"https://api.github.com/users/danneu","html_url":"https://github.com/danneu","followers_url":"https://api.github.com/users/danneu/followers","following_url":"https://api.github.com/users/danneu/following{/other_user}","gists_url":"https://api.github.com/users/danneu/gists{/gist_id}","starred_url":"https://api.github.com/users/danneu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danneu/subscriptions","organizations_url":"https://api.github.com/users/danneu/orgs","repos_url":"https://api.github.com/users/danneu/repos","events_url":"https://api.github.com/users/danneu/events{/privacy}","received_events_url":"https://api.github.com/users/danneu/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-08-20T06:58:23Z","updated_at":"2015-08-24T17:23:58Z","closed_at":null,"body":"The source in question: https://github.com/epochtalk/core-pg/blob/94cfacd4fefd36325f9ef2a6f03f4866010f483f/db.js#L40-L47\r\n\r\n``` javascript\r\n.disposer(function(client, promise) {\r\n  function closeConnection() { if (close) { close(client); } }\r\n\r\n  if (promise.isFulfilled()) {\r\n    return client.queryAsync('COMMIT').then(closeConnection);\r\n  }\r\n  else {\r\n    return client.queryAsync('ROLLBACK').then(closeConnection);\r\n  }\r\n});\r\n```\r\n\r\nIt looks like `client` is always going to be a truthy value here, and passing a truthy value into `close(truthy)` is how you remove a connection from the pool whereas `close()` releases the connection back into the pool for reuse.\r\n\r\nAs I understand this code, the connection will always be removed from the pool after successful COMMIT or successful ROLLBACK. Instead, those two cases should always call `close()` to release the connection since the connection is in a good state (committed or rolled back).\r\n\r\nHowever, ROLLBACK can fail. If ROLLBACK fails, then the connection is mid-transaction and it's *then* that it needs to be nuked from the pool with `close(truthy)`. Since the current code doesn't handle ROLLBACK failure, it will leak a bad connection instead of nuking it.\r\n\r\nI have a Node+Postgres forum running in production that attempts a similar abstraction: https://github.com/danneu/guild/blob/7506221c6004d5147c9139828242dc5459ee8a07/server/db.js#L97"},"comment":{"url":"https://api.github.com/repos/epochtalk/core-pg/issues/comments/134307343","html_url":"https://github.com/epochtalk/core-pg/issues/21#issuecomment-134307343","issue_url":"https://api.github.com/repos/epochtalk/core-pg/issues/21","id":134307343,"user":{"login":"danneu","id":529580,"avatar_url":"https://avatars.githubusercontent.com/u/529580?v=3","gravatar_id":"","url":"https://api.github.com/users/danneu","html_url":"https://github.com/danneu","followers_url":"https://api.github.com/users/danneu/followers","following_url":"https://api.github.com/users/danneu/following{/other_user}","gists_url":"https://api.github.com/users/danneu/gists{/gist_id}","starred_url":"https://api.github.com/users/danneu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danneu/subscriptions","organizations_url":"https://api.github.com/users/danneu/orgs","repos_url":"https://api.github.com/users/danneu/repos","events_url":"https://api.github.com/users/danneu/events{/privacy}","received_events_url":"https://api.github.com/users/danneu/received_events","type":"User","site_admin":false},"created_at":"2015-08-24T17:23:58Z","updated_at":"2015-08-24T17:23:58Z","body":"@taesup, @wangbus: The `if (close)` and `if (err)` checks seem extraneous to me at a glance, but otherwise it looks good."}}