Skip to content

Commit

Permalink
Clean DB properties on close (#123)
Browse files Browse the repository at this point in the history
* clean up references to DBs and Connections in pools
* Implement an explicit clear() method for clearing all the items in the pool.
  • Loading branch information
agroszer authored and jimfulton committed May 17, 2017
1 parent e714f51 commit e9c7492
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/ZODB/DB.py
Expand Up @@ -107,6 +107,10 @@ def getTimeout(self):

size = property(getSize, lambda self, v: self.setSize(v))

def clear(self):
pass


class ConnectionPool(AbstractConnectionPool):

def __init__(self, size, timeout=1<<31):
Expand Down Expand Up @@ -230,6 +234,11 @@ def availableGC(self):
self.available[:] = [i for i in self.available
if i[1] not in to_remove]

def clear(self):
while self.pop():
pass


class KeyedConnectionPool(AbstractConnectionPool):
# this pool keeps track of keyed connections all together. It makes
# it possible to make assertions about total numbers of keyed connections.
Expand Down Expand Up @@ -285,6 +294,11 @@ def availableGC(self):
if not pool.all:
del self.pools[key]

def clear(self):
for pool in self.pools.values():
pool.clear()
self.pools.clear()

@property
def test_all(self):
result = set()
Expand Down Expand Up @@ -645,6 +659,11 @@ def _(c):
self._mvcc_storage.close()
del self.storage
del self._mvcc_storage
# clean up references to other DBs
self.databases = {}
# clean up the connection pool
self.pool.clear()
self.historical_pool.clear()

def getCacheSize(self):
"""Get the configured cache size (objects).
Expand Down
2 changes: 1 addition & 1 deletion src/ZODB/tests/multidb.txt
Expand Up @@ -137,7 +137,7 @@ an exception:

Clean up:

>>> for a_db in dbmap.values():
>>> for a_db in list(dbmap.values()):
... a_db.close()


Expand Down
25 changes: 25 additions & 0 deletions src/ZODB/tests/testDB.py
Expand Up @@ -397,6 +397,31 @@ def minimally_test_connection_timeout():
"""

def cleanup_on_close():
"""Verify that various references are cleared on close
>>> db = ZODB.DB(None)
>>> conn = db.open()
>>> conn.root.x = 'x'
>>> transaction.commit()
>>> conn.close()
>>> historical_conn = db.open(at=db.lastTransaction())
>>> historical_conn.close()
>>> db.close()
>>> db.databases
{}
>>> db.pool.pop() is None
True
>>> [pool is None for pool in db.historical_pool.pools.values()]
[]
"""

def test_suite():
s = unittest.makeSuite(DBTests)
s.addTest(doctest.DocTestSuite(
Expand Down

0 comments on commit e9c7492

Please sign in to comment.