Skip to content
Snippets Groups Projects
Commit 1d9c0bf0 authored by vlorentz's avatar vlorentz
Browse files

Explicitely give Db connections back to the pool.

So they gracefully release the connection on error instead
of relying on reference-counting to call the Db's `__del__`
(which does not happen in Hypothesis tests) because a ref
to it is kept via the traceback object.
parent d54f0afb
No related branches found
Tags v0.0.59
1 merge request!55Explicitely give Db connections back to the pool.
......@@ -113,7 +113,7 @@ class BaseDb:
self.conn = conn
self.pool = pool
def __del__(self):
def put_conn(self):
if self.pool:
self.pool.putconn(self.conn)
......@@ -182,6 +182,7 @@ class BaseDb:
for k in columns]
f.write(','.join(line))
f.write('\n')
finally:
# No problem bubbling up exceptions, but we still need to make sure
# we finish copying, even though we're probably going to cancel the
......
......@@ -43,9 +43,12 @@ def db_transaction(**client_options):
return ret
else:
db = self.get_db()
with db.transaction() as cur:
apply_options(cur, __client_options)
return meth(self, *args, db=db, cur=cur, **kwargs)
try:
with db.transaction() as cur:
apply_options(cur, __client_options)
return meth(self, *args, db=db, cur=cur, **kwargs)
finally:
self.put_db(db)
return _meth
return decorator
......@@ -73,8 +76,12 @@ def db_transaction_generator(**client_options):
apply_options(cur, old_options)
else:
db = self.get_db()
with db.transaction() as cur:
apply_options(cur, __client_options)
yield from meth(self, *args, db=db, cur=cur, **kwargs)
try:
with db.transaction() as cur:
apply_options(cur, __client_options)
yield from meth(self, *args, db=db, cur=cur, **kwargs)
finally:
self.put_db(db)
return _meth
return decorator
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment