diff --git a/rhc/database/db.py b/rhc/database/db.py index 800d7ef..903021d 100644 --- a/rhc/database/db.py +++ b/rhc/database/db.py @@ -22,9 +22,13 @@ THE SOFTWARE. ''' import pymysql +import logging import threading +log = logging.getLogger(__name__) + + class _DB(object): def __init__(self): @@ -86,6 +90,10 @@ def start_transaction(self): self.__transaction += 1 def stop_transaction(self, commit=True): + log.debug( + 'starting `stop_transaction`, self.__transaction=%s', + self.__transaction, + ) if self.__transaction == 0: raise Exception('attempting to stop transaction when none is started') self.__transaction -= 1 @@ -96,6 +104,10 @@ def stop_transaction(self, commit=True): self._rollback() if self.__close: self.close() + log.debug( + 'finishing `stop_transaction`, self.__transaction=%s', + self.__transaction, + ) def database_map(self, tablename): return self.__database_map.get(tablename, tablename) diff --git a/rhc/database/test/test_basic.py b/rhc/database/test/test_basic.py index c5ad997..eea72ef 100644 --- a/rhc/database/test/test_basic.py +++ b/rhc/database/test/test_basic.py @@ -1,5 +1,6 @@ from __future__ import absolute_import import pytest +import weakref from rhc.database.dao import DAO @@ -103,3 +104,22 @@ def test_join(data): assert len(names) == 2 assert 'fred' in names assert 'sally' in names + + +def test_dao_cleanup_from_save(db): + obj = Parent(foo=1, bar=2).save() + ref = weakref.ref(obj) + assert obj is ref() + obj = 0 # dereference + assert ref() is None + + +def test_dao_cleanup_from_load(db): + # save object and remember id + obj_id = Parent(foo=1, bar=2).save().id + assert obj_id + obj = Parent.load(obj_id) + ref = weakref.ref(obj) + assert ref() is obj + obj = 0 + assert ref() is None