Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions rhc/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
THE SOFTWARE.
'''
import pymysql
import logging
import threading


log = logging.getLogger(__name__)


class _DB(object):

def __init__(self):
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions rhc/database/test/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import pytest
import weakref

from rhc.database.dao import DAO

Expand Down Expand Up @@ -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