[Zope3-checkins] CVS: Zope3/src/transaction - _transaction.py:1.1.2.24

Jeremy Hylton jeremy at zope.com
Tue Mar 23 16:03:13 EST 2004


Update of /cvs-repository/Zope3/src/transaction
In directory cvs.zope.org:/tmp/cvs-serv8211

Modified Files:
      Tag: jeremy-txn-branch
	_transaction.py 
Log Message:
An it-works-for-now hack: Restore the DataManagerAdapter from ZODB3.


=== Zope3/src/transaction/_transaction.py 1.1.2.23 => 1.1.2.24 ===
--- Zope3/src/transaction/_transaction.py:1.1.2.23	Tue Mar 23 15:49:02 2004
+++ Zope3/src/transaction/_transaction.py	Tue Mar 23 16:03:12 2004
@@ -138,6 +138,11 @@
         if self.status != Status.ACTIVE:
             raise ValueError("expected txn status %r, but it's %r" % (
                              Status.ACTIVE, self.status))
+        # XXX the prepare check is a bit of a hack, perhaps it would
+        # be better to use interfaces.  If this is a ZODB4-style
+        # resource manager, it needs to be adapted, too.
+        if myhasattr(resource, "prepare"):
+            resource = DataManagerAdapter(resource)
         self._resources.append(resource)
 
     def register(self, obj):
@@ -281,7 +286,7 @@
                 # XXX I think _sub and _nonsub are disjoint, and that
                 # XXX _resources is empty.  If so, we can simplify this code.
                 assert len(d) == len(self._sub) + len(self._nonsub)
-                assert not self._resources, (self._resources, self._sub, self._nonsub)
+                assert not self._resources
                 for rm in self._resources:
                     d[id(rm)] = rm
                 L = d.values()
@@ -456,3 +461,60 @@
         oid = oid_repr(oid)
     return "%s oid=%s" % (klass, oid)
 
+class DataManagerAdapter(object):
+    """Adapt zodb 4-style data managers to zodb3 style
+
+    Adapt transaction.interfaces.IDataManager to
+    ZODB.interfaces.IPureDatamanager
+    """
+
+    # Note that it is pretty important that this does not have a _p_jar
+    # attribute. This object will be registered with a zodb3 TM, which
+    # will then try to get a _p_jar from it, using it as the default.
+    # (Objects without a _p_jar are their own data managers.)
+
+    def __init__(self, datamanager):
+        self._datamanager = datamanager
+        self._rollback = None
+
+    def commit(self, ob, transaction):
+        assert ob is self
+
+    def abort(self, ob, transaction):
+        assert ob is self
+
+        # We need to discard any changes since the last save point, or all
+        # changes
+
+        if self._rollback is None:
+            # No previous savepoint, so just abort
+            self._datamanager.abort(transaction)
+        else:
+            self._rollback()
+
+    def abort_sub(self, transaction):
+        self._datamanager.abort(transaction)
+
+    def commit_sub(self, transaction):
+        # Nothing to do wrt data, be we begin 2pc for the top-level
+        # trans
+        self._sub = False
+        
+    def tpc_begin(self, transaction, subtransaction=False):
+        self._sub = subtransaction
+
+    def tpc_abort(self, transaction):
+        if self._sub:
+            self.abort(self, transaction)
+        else:
+            self._datamanager.abort(transaction)
+
+    def tpc_finish(self, transaction):
+        if self._sub:
+            self._rollback = self._datamanager.savepoint(transaction).rollback
+        else:
+            self._datamanager.commit(transaction)
+
+    def tpc_vote(self, transaction):
+        if not self._sub:
+            self._datamanager.prepare(transaction)




More information about the Zope3-Checkins mailing list