Fixes pudo/dataset#363 by tracking checked-out sqlalchemy database connections, on a per-thread basis, and closing all connections when Database.close() is called.
This commit is contained in:
parent
2791a61939
commit
ed3b120f85
@ -44,6 +44,7 @@ class Database(object):
|
|||||||
|
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
self.local = threading.local()
|
self.local = threading.local()
|
||||||
|
self.connections = {}
|
||||||
|
|
||||||
if len(parsed_url.query):
|
if len(parsed_url.query):
|
||||||
query = parse_qs(parsed_url.query)
|
query = parse_qs(parsed_url.query)
|
||||||
@ -76,9 +77,11 @@ class Database(object):
|
|||||||
@property
|
@property
|
||||||
def executable(self):
|
def executable(self):
|
||||||
"""Connection against which statements will be executed."""
|
"""Connection against which statements will be executed."""
|
||||||
if not hasattr(self.local, "conn"):
|
with self.lock:
|
||||||
self.local.conn = self.engine.connect()
|
tid = threading.get_ident()
|
||||||
return self.local.conn
|
if not tid in self.connections:
|
||||||
|
self.connections[tid] = self.engine.connect()
|
||||||
|
return self.connections[tid]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def op(self):
|
def op(self):
|
||||||
@ -158,6 +161,10 @@ class Database(object):
|
|||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
"""Close database connections. Makes this object unusable."""
|
"""Close database connections. Makes this object unusable."""
|
||||||
|
with self.lock:
|
||||||
|
for conn in self.connections.values():
|
||||||
|
conn.close()
|
||||||
|
self.connections.clear()
|
||||||
self.engine.dispose()
|
self.engine.dispose()
|
||||||
self._tables = {}
|
self._tables = {}
|
||||||
self.engine = None
|
self.engine = None
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user