[Zope] Unique incrementing IDs

Michel Pelletier michel@digicool.com
Sat, 11 Sep 1999 13:09:07 -0400


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?

Yes.  This would cause the database to raise a 'ConflictError'.  If this
happes, one thread wins and one thread looses.  The looser's transaction
is completely aborted, and the loosers request is resubmitted in a whole
new transaction.  This works well to avoid the problem your thinking
about.

-Michel