diff options
author | Julien Danjou <julien@danjou.info> | 2017-06-09 09:53:08 +0200 |
---|---|---|
committer | Julien Danjou <julien@danjou.info> | 2017-06-16 11:40:42 +0000 |
commit | ee66b6e4e416569cb8d51a9b19263c22c6e1f87d (patch) | |
tree | 7b08c6b0d05282797180527e9677b8fe45c5be01 | |
parent | 5625106703e1c018a04eb1bd01b1732e637dd950 (diff) | |
download | tooz-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
-rw-r--r-- | tooz/drivers/mysql.py | 9 | ||||
-rw-r--r-- | tooz/drivers/pgsql.py | 45 |
2 files changed, 29 insertions, 25 deletions
diff --git a/tooz/drivers/mysql.py b/tooz/drivers/mysql.py index a86a3dc..72853d7 100644 --- a/tooz/drivers/mysql.py +++ b/tooz/drivers/mysql.py @@ -68,7 +68,6 @@ class MySQLLock(locking.Lock): self.acquired = True return True except pymysql.MySQLError as e: - self._conn.close() utils.raise_with_cause( tooz.ToozError, encodeutils.exception_to_unicode(e), @@ -79,7 +78,13 @@ class MySQLLock(locking.Lock): self._conn.close() return False - return _lock() + try: + return _lock() + except Exception: + # Close the connection if we tried too much and finally failed, or + # anything else bad happened. + self._conn.close() + raise def release(self): if not self.acquired: 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: |