summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tooz/drivers/mysql.py9
-rw-r--r--tooz/drivers/pgsql.py45
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):
68 self.acquired = True 68 self.acquired = True
69 return True 69 return True
70 except pymysql.MySQLError as e: 70 except pymysql.MySQLError as e:
71 self._conn.close()
72 utils.raise_with_cause( 71 utils.raise_with_cause(
73 tooz.ToozError, 72 tooz.ToozError,
74 encodeutils.exception_to_unicode(e), 73 encodeutils.exception_to_unicode(e),
@@ -79,7 +78,13 @@ class MySQLLock(locking.Lock):
79 self._conn.close() 78 self._conn.close()
80 return False 79 return False
81 80
82 return _lock() 81 try:
82 return _lock()
83 except Exception:
84 # Close the connection if we tried too much and finally failed, or
85 # anything else bad happened.
86 self._conn.close()
87 raise
83 88
84 def release(self): 89 def release(self):
85 if not self.acquired: 90 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):
123 if not self._conn or self._conn.closed: 123 if not self._conn or self._conn.closed:
124 self._conn = PostgresDriver.get_connection(self._parsed_url, 124 self._conn = PostgresDriver.get_connection(self._parsed_url,
125 self._options) 125 self._options)
126 try: 126
127 with _translating_cursor(self._conn) as cur: 127 with _translating_cursor(self._conn) as cur:
128 if blocking is True: 128 if blocking is True:
129 cur.execute("SELECT pg_advisory_lock(%s, %s);", 129 cur.execute("SELECT pg_advisory_lock(%s, %s);",
130 self.key) 130 self.key)
131 cur.fetchone() 131 cur.fetchone()
132 self.acquired = True
133 return True
134 else:
135 cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
136 self.key)
137 if cur.fetchone()[0] is True:
132 self.acquired = True 138 self.acquired = True
133 return True 139 return True
140 elif blocking is False:
141 self._conn.close()
142 return False
134 else: 143 else:
135 cur.execute("SELECT pg_try_advisory_lock(%s, %s);", 144 raise _retry.TryAgain
136 self.key) 145
137 if cur.fetchone()[0] is True: 146 try:
138 self.acquired = True 147 return _lock()
139 return True 148 except Exception:
140 elif blocking is False: 149 self._conn.close()
141 self._conn.close() 150 raise
142 return False
143 else:
144 raise _retry.TryAgain
145 except _retry.TryAgain:
146 pass # contine to retrieve lock on same conn
147 except Exception:
148 self._conn.close()
149 raise
150
151 return _lock()
152 151
153 def release(self): 152 def release(self):
154 if not self.acquired: 153 if not self.acquired: