Commit fc8f8191 authored by Jim Fulton's avatar Jim Fulton

Implemented explicit mode

parent 8e45562b
......@@ -21,7 +21,9 @@ import threading
from zope.interface import implementer
from transaction.interfaces import AlreadyInTransaction
from transaction.interfaces import ITransactionManager
from transaction.interfaces import NoTransaction
from transaction.interfaces import TransientError
from transaction.weakset import WeakSet
from transaction._compat import reraise
......@@ -59,7 +61,8 @@ def _new_transaction(txn, synchs):
@implementer(ITransactionManager)
class TransactionManager(object):
def __init__(self):
def __init__(self, explicit=False):
self.explicit = explicit
self._txn = None
self._synchs = WeakSet()
......@@ -67,6 +70,8 @@ class TransactionManager(object):
""" See ITransactionManager.
"""
if self._txn is not None:
if self.explicit:
raise AlreadyInTransaction()
self._txn.abort()
txn = self._txn = Transaction(self._synchs, self)
_new_transaction(txn, self._synchs)
......@@ -78,6 +83,8 @@ class TransactionManager(object):
""" See ITransactionManager.
"""
if self._txn is None:
if self.explicit:
raise NoTransaction()
self._txn = Transaction(self._synchs, self)
return self._txn
......
......@@ -32,7 +32,7 @@ class ITransactionManager(Interface):
def begin():
"""Explicitly begin a new transaction.
"""Explicitly begin and return a new transaction.
If an existing transaction is in progress and the transaction
manager not in explicit mode, the previous transaction will be
......
......@@ -711,6 +711,42 @@ class AttemptTests(unittest.TestCase):
self.assertFalse(manager.committed)
self.assertTrue(manager.aborted)
def test_explicit_mode(self):
from .. import TransactionManager
from ..interfaces import AlreadyInTransaction, NoTransaction
tm = TransactionManager()
self.assertFalse(tm.explicit)
tm = TransactionManager(explicit=True)
self.assertTrue(tm.explicit)
for name in 'get', 'commit', 'abort', 'doom', 'isDoomed', 'savepoint':
with self.assertRaises(NoTransaction):
getattr(tm, name)()
t = tm.begin()
with self.assertRaises(AlreadyInTransaction):
tm.begin()
self.assertTrue(t is tm.get())
self.assertFalse(tm.isDoomed())
tm.doom()
self.assertTrue(tm.isDoomed())
tm.abort()
for name in 'get', 'commit', 'abort', 'doom', 'isDoomed', 'savepoint':
with self.assertRaises(NoTransaction):
getattr(tm, name)()
t = tm.begin()
self.assertFalse(tm.isDoomed())
with self.assertRaises(AlreadyInTransaction):
tm.begin()
tm.savepoint()
tm.commit()
class DummyManager(object):
entered = False
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment