Skip to content

Commit

Permalink
Cleaning up transaction state checking. Issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshultz committed Dec 9, 2017
1 parent bcde76b commit ea77690
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions rawl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
from psycopg2.pool import ThreadedConnectionPool
from psycopg2.extensions import (
ISOLATION_LEVEL_READ_COMMITTED,
TRANSACTION_STATUS_INTRANS
TRANSACTION_STATUS_INTRANS,
TRANSACTION_STATUS_ACTIVE,
TRANSACTION_STATUS_INERROR
)

OPEN_TRANSACTION_STATES = (TRANSACTION_STATUS_INTRANS,
TRANSACTION_STATUS_ACTIVE, TRANSACTION_STATUS_INERROR)

log = logging.getLogger('rawl')

_POOL = None
Expand Down Expand Up @@ -49,9 +54,7 @@ def __enter__(self):
log.info("Connecting to %s" % self.dsn)

self.conn = _POOL.getconn()
if self.conn.get_transaction_status() not in (
TRANSACTION_STATUS_INTRANS, TRANSACTION_STATUS_ACTIVE,
TRANSACTION_STATUS_INERROR):
if self.conn.get_transaction_status() not in OPEN_TRANSACTION_STATES:
self.conn.set_session(isolation_level=ISOLATION_LEVEL_READ_COMMITTED)
return self.conn

Expand All @@ -60,7 +63,7 @@ def __enter__(self):

finally:
# Assume rolled back if uncommitted
if self.conn.get_transaction_status() == TRANSACTION_STATUS_INTRANS:
if self.conn.get_transaction_status() in OPEN_TRANSACTION_STATES:
self.conn.rollback()
_POOL.putconn(self.conn)
self.conn = None
Expand Down

0 comments on commit ea77690

Please sign in to comment.