Commit 1248b5dd authored by Jeremy Hylton's avatar Jeremy Hylton

Add IllegalStateError calls to prevent assertions from failing.

XXX Should the manager duplicate these checks?
parent d35cc16d
...@@ -3,14 +3,21 @@ ...@@ -3,14 +3,21 @@
__metaclass__ = type __metaclass__ = type
from transaction.interfaces import ITransaction, TransactionError from transaction.interfaces import *
from threading import Lock from threading import Lock
class Set(dict): try:
from sets import Set
except ImportError:
class Set(dict):
def add(self, k): def add(self, k):
self[k] = 1 self[k] = 1
def remove(self, k):
del self[k]
class Status: class Status:
ACTIVE = "Active" ACTIVE = "Active"
...@@ -50,24 +57,29 @@ class Transaction: ...@@ -50,24 +57,29 @@ class Transaction:
def commit(self): def commit(self):
"""Commit a transaction.""" """Commit a transaction."""
assert self._manager is not None assert self._manager is not None
if self._status != Status.ACTIVE:
raise IllegalStateError("commit", self._status)
self._manager.commit(self) self._manager.commit(self)
def abort(self): def abort(self):
"""Rollback to initial state.""" """Rollback to initial state."""
assert self._manager is not None assert self._manager is not None
if self._status not in (Status.ACTIVE, Status.PREPARED, Status.FAILED):
raise IllegalStateError("abort", self._status)
self._manager.abort(self) self._manager.abort(self)
def savepoint(self): def savepoint(self):
"""Save current progress and return a savepoint.""" """Save current progress and return a savepoint."""
assert self._manager is not None assert self._manager is not None
if self._status != Status.ACTIVE:
raise IllegalStateError("create savepoint", self._status)
return self._manager.savepoint(self) return self._manager.savepoint(self)
def join(self, resource): def join(self, resource):
"""resource is participating in the transaction.""" """resource is participating in the transaction."""
assert self._manager is not None assert self._manager is not None
if self._status != Status.ACTIVE: if self._status != Status.ACTIVE:
raise TransactionError("Can't join transaction. Status=%s" % raise IllegalStateError("join", self._status)
self._status)
self._resources.add(resource) self._resources.add(resource)
def status(self): def status(self):
...@@ -78,7 +90,7 @@ class Transaction: ...@@ -78,7 +90,7 @@ class Transaction:
self._lock.acquire() self._lock.acquire()
try: try:
if self._status == Status.SUSPENDED: if self._status == Status.SUSPENDED:
raise TransactionError("Already suspended") raise IllegalStateError("suspend", self._status)
self._manager.suspend(self) self._manager.suspend(self)
self._suspend = self._status self._suspend = self._status
self._status = Status.SUSPENDED self._status = Status.SUSPENDED
......
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