[ZODB-Dev] zodb4: adding an object before transaction commit

Steve Alexander steve at z3u.com
Fri Oct 10 06:21:29 EDT 2003


>>I chatted briefly with Jim Fulton about this. An API such as 
>>Connection.add(object) was suggested.
> 
> We would find such an API useful, I think.

This API is now in ZODB 4.

Jim Fulton has said that a similar API can be added to ZODB 3.3 (as 
counted by releases -- this may not be the release that supports 
new-style classes rather than extension class).

I have some time to work on this for ZODB 3. But, perhaps some others 
would like to help?

Here's the add method for Connection in ZODB 4:

     def add(self, obj):
         marker = object()
         oid = getattr(obj, "_p_oid", marker)
         if oid is marker:
             raise TypeError("cannot add a non-persistent object %r "
                             "to a connection" % (obj, ))
         if obj._p_jar is not None and obj._p_jar is not self:
             raise InvalidObjectReference(obj, obj._p_jar)
         if obj._p_jar is None:
             # Setting _p_changed has a side-effect of adding obj to
             # _p_jar._registered, so it must be set after _p_jar.
             obj._p_jar = self
             obj._p_oid = self.newObjectId()
             obj._p_changed = True
             self._created.add(obj._p_oid)

             # There is an 'invariant' that objects in the cache can be
             # made into ghosts because they have _p_jar and _p_oid.
             # We are breaking this invariant, but that's OK because
             # it's not used anywhere.  The right solution is to have a
             # separate cache of objects outside that invariant.
             self._cache.set(obj._p_oid, obj)


There are also changes to the interface and unit tests in ZODB 4.

--
Steve Alexander




More information about the ZODB-Dev mailing list