summaryrefslogtreecommitdiff
path: root/tooz/drivers/pgsql.py
diff options
context:
space:
mode:
authorJulien Danjou <julien@danjou.info>2017-06-09 09:53:08 +0200
committerJulien Danjou <julien@danjou.info>2017-06-16 11:40:42 +0000
commitee66b6e4e416569cb8d51a9b19263c22c6e1f87d (patch)
tree7b08c6b0d05282797180527e9677b8fe45c5be01 /tooz/drivers/pgsql.py
parent5625106703e1c018a04eb1bd01b1732e637dd950 (diff)
downloadtooz-ee66b6e4e416569cb8d51a9b19263c22c6e1f87d.tar.gz
{my,pg}sql: close connections when out of retry1.57.2
The connection is supposedly closed when blocking is False and the lock is not acquired. Or when an exception happens in the locking code. But if blocking is e.g. 5 seconds, then RetryError is raised but without closing any connection at the end. This fixes that. Change-Id: I7b40e466fe5fc01ee3f8c012f765aafca9ce716a
Diffstat (limited to 'tooz/drivers/pgsql.py')
-rw-r--r--tooz/drivers/pgsql.py45
1 files changed, 22 insertions, 23 deletions
diff --git a/tooz/drivers/pgsql.py b/tooz/drivers/pgsql.py
index 6e68617..ee92db1 100644
--- a/tooz/drivers/pgsql.py
+++ b/tooz/drivers/pgsql.py
@@ -123,32 +123,31 @@ class PostgresLock(locking.Lock):
if not self._conn or self._conn.closed:
self._conn = PostgresDriver.get_connection(self._parsed_url,
self._options)
- try:
- with _translating_cursor(self._conn) as cur:
- if blocking is True:
- cur.execute("SELECT pg_advisory_lock(%s, %s);",
- self.key)
- cur.fetchone()
+
+ with _translating_cursor(self._conn) as cur:
+ if blocking is True:
+ cur.execute("SELECT pg_advisory_lock(%s, %s);",
+ self.key)
+ cur.fetchone()
+ self.acquired = True
+ return True
+ else:
+ cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
+ self.key)
+ if cur.fetchone()[0] is True:
self.acquired = True
return True
+ elif blocking is False:
+ self._conn.close()
+ return False
else:
- cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
- self.key)
- if cur.fetchone()[0] is True:
- self.acquired = True
- return True
- elif blocking is False:
- self._conn.close()
- return False
- else:
- raise _retry.TryAgain
- except _retry.TryAgain:
- pass # contine to retrieve lock on same conn
- except Exception:
- self._conn.close()
- raise
-
- return _lock()
+ raise _retry.TryAgain
+
+ try:
+ return _lock()
+ except Exception:
+ self._conn.close()
+ raise
def release(self):
if not self.acquired: