{"action":"created","issue":{"url":"https://api.github.com/repos/coleifer/peewee/issues/1093","repository_url":"https://api.github.com/repos/coleifer/peewee","labels_url":"https://api.github.com/repos/coleifer/peewee/issues/1093/labels{/name}","comments_url":"https://api.github.com/repos/coleifer/peewee/issues/1093/comments","events_url":"https://api.github.com/repos/coleifer/peewee/issues/1093/events","html_url":"https://github.com/coleifer/peewee/issues/1093","id":181828131,"number":1093,"title":"Unable to except OperationalError / connection trying to be closed after catch block","user":{"login":"theotherp","id":14368889,"avatar_url":"https://avatars.githubusercontent.com/u/14368889?v=3","gravatar_id":"","url":"https://api.github.com/users/theotherp","html_url":"https://github.com/theotherp","followers_url":"https://api.github.com/users/theotherp/followers","following_url":"https://api.github.com/users/theotherp/following{/other_user}","gists_url":"https://api.github.com/users/theotherp/gists{/gist_id}","starred_url":"https://api.github.com/users/theotherp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/theotherp/subscriptions","organizations_url":"https://api.github.com/users/theotherp/orgs","repos_url":"https://api.github.com/users/theotherp/repos","events_url":"https://api.github.com/users/theotherp/events{/privacy}","received_events_url":"https://api.github.com/users/theotherp/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-10-08T14:10:28Z","updated_at":"2016-10-10T20:04:40Z","closed_at":"2016-10-09T20:22:12Z","body":"This is what my code looks like:\r\n\r\n```\r\ntry:\r\n    searchResult, _ = tryGetOrCreateSearchResultDbEntry(indexer.indexer.id, result)\r\n    result.searchResultId = searchResult.id\r\n    search_results.append(result)\r\nexcept (IntegrityError, OperationalError) as e:\r\n    logger.error(\"Error while trying to save search result to database. Skipping it. Error: %s\" % e)\r\n```\r\n\r\nand this the stacktrace:\r\n\r\n```\r\n2016-10-08 16:05:21,416 - ERROR - web - cannot commit - no transaction is active\r\nTraceback (most recent call last):\r\n  File \"/home/snowcrash/nzbhydra/nzbhydra/web.py\", line 570, in api_search\r\n    result = search.search(search_request)\r\n  File \"/home/snowcrash/nzbhydra/nzbhydra/search.py\", line 318, in search\r\n    logger.error(\"Error while trying to save search result to database. Skipping it. Error: %s\" % e)\r\n  File \"/home/snowcrash/nzbhydra/libs/peewee.py\", line 4314, in __exit__\r\n    return self._helper.__exit__(exc_type, exc_val, exc_tb)\r\n  File \"/home/snowcrash/nzbhydra/libs/peewee.py\", line 4348, in __exit__\r\n    self.commit(False)\r\n  File \"/home/snowcrash/nzbhydra/libs/peewee.py\", line 4325, in commit\r\n    self.db.commit()\r\n  File \"/home/snowcrash/nzbhydra/libs/peewee.py\", line 3711, in commit\r\n    self.get_conn().commit()\r\nOperationalError: cannot commit - no transaction is active\r\n```\r\n\r\nSo some part of the code magically tries to commit and fails. I'd like to ignore the OperationalError (as I'm unable to completely prevent it) that happens while trying to access the db but can't catch that exception, then it's raised in the catch block. I even tried disabling autocommit before and committing manually but even then the error occurred.\r\n\r\nEdit: I managed to get rid of all errors by using a thread lock for the main part of the code that writes to the database. It increases the run time but that's worth it. I leave it up to you to close this or investigate further."},"comment":{"url":"https://api.github.com/repos/coleifer/peewee/issues/comments/252731753","html_url":"https://github.com/coleifer/peewee/issues/1093#issuecomment-252731753","issue_url":"https://api.github.com/repos/coleifer/peewee/issues/1093","id":252731753,"user":{"login":"coleifer","id":119974,"avatar_url":"https://avatars.githubusercontent.com/u/119974?v=3","gravatar_id":"","url":"https://api.github.com/users/coleifer","html_url":"https://github.com/coleifer","followers_url":"https://api.github.com/users/coleifer/followers","following_url":"https://api.github.com/users/coleifer/following{/other_user}","gists_url":"https://api.github.com/users/coleifer/gists{/gist_id}","starred_url":"https://api.github.com/users/coleifer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coleifer/subscriptions","organizations_url":"https://api.github.com/users/coleifer/orgs","repos_url":"https://api.github.com/users/coleifer/repos","events_url":"https://api.github.com/users/coleifer/events{/privacy}","received_events_url":"https://api.github.com/users/coleifer/received_events","type":"User","site_admin":false},"created_at":"2016-10-10T20:04:40Z","updated_at":"2016-10-10T20:04:40Z","body":"So the issue was this, as I understand it.\r\n\r\nEach database driver (sqlite3, pymysql, psycopg2, etc) implements all or most of the DB-API 2.0 spec (PEP-249). This PEP outlines a handful of exception types. What Peewee does is it *wraps* the vendor-specific exception classes so that you only need to worry about catching the exceptions defined in Peewee itself. This allows portability between db implementations, e.g., I can catch `peewee.OperationalError` instead of having to catch `sqlite3.OperationalError` or `psycopg2.OperationalError`. Unfortunately, though, there was no exception wrapping taking place on the methods where Peewee was calling \"commit\" and \"rollback\" directly on the database driver. This meant that if you called commit and it triggered an OperationalError, you would get the \"vendor-specific\" version as opposed to the peewee version. **That** bug is what the above commit, 900ed22, fixes."}}
{"actor":{"display_login":"coleifer"}}