[Zope] Unique incrementing IDs

Martijn Pieters mj@antraciet.nl
Sat, 11 Sep 1999 17:25:25 +0200


At 16:46 11-9-99 , Kevin Dangoor wrote:
>Hi,
>
>     Since Zope is multithreaded, it seems like there could be a problem
>doing something like this:
>
><dtml-comment newid is a folder property>
><dtml-call "REQUEST.set('newid', newid + 1)">
><dtml-call "manage_changeProperties(REQUEST)">
><dtml-call "REQUEST.set('id', newid)">
><dtml-with "manage_addProduct['SomeProduct']">
><dtml-call "ZClass_add(_.None, _)">
></dtml-with>
>
>It seems plausible that you might end up trying to create two objects with
>the same ID. This will obviously fail, but it would be nice if there was an
>easy way to get the next ID sequentially like this. Am I correct in assuming
>that there may be a condition where it tries to create two objects with the
>same ID?
>
>Kevin

No, Zope is protected against this kind of thing. Two concepts together 
prevent this:

1. Transactions: All your changes are saved as one. One exception in the 
code and nothing is saved.

2. If one thread tries to access data in the ODB, and that data is then 
modified by another thread, a ConflictError is raised in the first, and it 
will try to do everything again with the updated data. It will try, iirc, 5 
times or so.

So if two threads execute above code, as soon as one of them commits, the 
other thread will have to start over with the new data.

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