[ZODB-Dev] BTrees and Mutables, was Re: [IndexedCatalog] bug in default indexing

Shane Hathaway shane@zope.com
Mon, 03 Mar 2003 16:07:34 -0500


Christian Reis wrote:
> On Mon, Mar 03, 2003 at 02:00:02PM -0500, Nicholas Henke wrote:
> 
>>>>+            index[value] = idx
>>>>+            idx.append(object)
>>>
>>>Shouldn't these two lines be reversed?
>>
>>Nope -- here is a wuote from the ZODB part of this thread ( the Re:
>>[ZODB-Dev] BTrees and Mutables). This is from Shane Hathaway, dated
>>16.21 03/02/11
>>
>>That's correct, although the purist way is like this:
>>
>>     l = tree['bar']
>>     tree['bar'] = l
>>     l.append(4)
>>
>>That way the notification happens before the actual change.  If an 
>>exception happens and the transaction is aborted, the changed list will 
>>be reverted.
> 
> 
> Hmmm. I wonder if this means that the append() can raise an exception.
> 
> I don't understand how this could make any difference. Which instruction
> might trigger an exception? What should it matter if abort() is issued
> -- the state will be reset to the original state anyway, won't it?

Let's say you write it like this, the non-purist way:

      L = tree['bar']
      L.append(4)
      tree['bar'] = L

Now imagine what happens if the last line fails for whatever reason 
without notifying ZODB that some bucket in 'tree' has changed.  The 
exception will probably propagate to something that aborts the 
transaction.  But since no one ever told ZODB that the tree['bar'] has 
changed, that list stays out of sync with the database.

In practice, this is rarely an issue.  Sometimes I correct code that 
does it in the wrong order, but usually I don't bother, because 
assigning to a persistent container usually either succeeds or notifies 
ZODB and then fails.

Shane