[Zope-dev] Re: [Zope] Question about ZODB and threads

Martijn Pieters mj@antraciet.nl
Thu, 14 Oct 1999 10:00:38 +0200


At 04:07 14/10/99 , Pavlos Christoforou wrote:
>Hello Zopistas -
>
>Suppose we have a persistent object registered with ZODB which has a list
>attribute contents. According to ZODB rules we should treat mutable
>attributes as immutable, but what happens if we are crazy enough to treat
>them as mutable and do things like:
>
>self.contents.append('crazy')
>
>Had we treated it as immutable we would have done something like:
>
>tmp=self.contents
>tmp.append('not so crazy')
>self.contents=tmp
>
>
>in which case tmp is local to the thread and the last statement triggers
>ZODB's thread-safe transaction mechanism. However in the first case ZODB
>is unaware of the modification. This was not a problem before the
>concurrent ZODB, but I am not sure what would happen if two threads call
>this function simultaneously. Any ideas? (No it is not a theoretical
>question. Such a construct can be very useful in many situations where you
>might want to store data in memory temporarily.)

(I moved this to Zope-Dev).

First of all: two threads each get their own copy of the class instance, so 
you are safe there, they will not interfere with each other. It is at 
commit time to the DB that things might conflict, but that's where the ZODB 
has it covered.

The fact that the ZODB isn't aware of the change will only cause it not to 
be committed. Your change will not be visible in any other thread, and will 
be gone as soon as the object is removed from memory (at least, that's my 
understanding).

You can make the ZODB aware of the changes you made by either changing a 
mutable on the object, or by setting _p_changed to a true value. Either way 
the object will be registered with the current transaction and the changes 
will go though the normal ZODB machinery, including conflict resolution.

All this can be found in the ZODB UML model at:

   http://www.zope.org/Documentation/Developer/Models/ZODB

especially, Persistent Objects (top left frame) -> Persistent (bottem left 
frame) and then read the bottom right frame. Don't you love this setup? =)


--
Martijn Pieters, Web Developer
| Antraciet http://www.antraciet.nl
| Tel: +31-35-7502100 Fax: +31-35-7502111
| mailto:mj@antraciet.nl http://www.antraciet.nl/~mj
| PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149
------------------------------------------