Skip to content

Commit

Permalink
Catching all syncronous errors. Fixes FSX#134.
Browse files Browse the repository at this point in the history
Things like `cursor.execute` can throw not only `psycopg2.Error`
exeception and their derivatives, but also standard Python errors
like `IndexError` bacause badly formatted SQL. In the latter case,
after such exception the connection became stale (i.e. forever-busy)
in the Pool.
  • Loading branch information
haizaar committed Nov 30, 2015
1 parent 7cc96cd commit 7dcc245
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion momoko/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def when_available(fut):
log.debug("Obtained connection: %s", conn.fileno)
try:
future_or_result = method(conn, *args, **kwargs)
except psycopg2.Error as error:
except Exception as error:
log.debug("Method failed synchronously")
return self._retry(retry, when_available, conn, keep, future)

Expand Down
9 changes: 9 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ def test_getconn_manage_with_exception(self):
pass
self.assertEqual(len(self.db.conns.busy), 0, msg="Some connections were not recycled")

@gen_test
def test_non_psycopg2_errors(self):
"""Testing that non-psycopg2 errors are catched properly"""
try:
sql = yield self.conn.execute("SELECT %s %s;", (1,))
except IndexError:
pass
self.assertEqual(len(self.db.conns.busy), 0, msg="Some connections were not recycled")


class MomokoPoolDataTestProxy(ProxyMixIn, MomokoPoolDataTest):
pass
Expand Down

0 comments on commit 7dcc245

Please sign in to comment.