[Zope] Generating unique keys

Stuart 'Zen' Bishop zen@cs.rmit.edu.au
Sun, 10 Oct 1999 02:07:04 +1000 (EST)


On Sat, 9 Oct 1999, Bill Anderson wrote:

> Stuart 'Zen' Bishop wrote:
> > 
> > I need to generate unique ID's for objects I'll be creating from DTML. This
> > of course needs to be thread safe. Although I can't do this out of the box
> > from within DTML (that I know of), I could get thread safety using an
> > external method, or a persistant object written in Python, and using locks
> > provided by the Python thread module. I think a persistant object (lets call
> > it ZSequence) would be best.
> 
> Have you seen my How-To on generating Unique ids?
> It may not be _exactly_ what you are looking for, but it will provide a
> decent starting place.
    
But What happens when two ID's are generated at exactly the same time through
different threads?

I found a gem that I thinks answers my queries at:
    http://www.zope.org/Documentation/Developer/Models/ZODB/ZODB_Doc.html

"When a transaction is to be committed, all of the objects modified by
 the transaction are checked to see if they have been invalidated. If
 any objects modified by the transaction have been invalidated, then the
 transaction is aborted and a ZODB3.ConflictError exception is raised.
 An application (e.g. the Python Object Publisher) should catch the
 ZODB3.Conflict exception and attempt to re-execute the transaction."

So an object can simply have an integer counter that is incremented
as necessary, and if two threads try to modify the counter at about the same 
time, the one that tries to commit last will rollback and be reexecuted.
If this is true, its so trivial a counter class and locking is totally 
unnecessary :-) And it should work with ZEO as well from what I understand
of its architecture.

 ___
   //     Zen (alias Stuart Bishop)     Work: zen@cs.rmit.edu.au
  // E N  Senior Systems Alchemist      Play: zen@shangri-la.dropbear.id.au
 //__     Computer Science, RMIT 	 WWW: http://www.cs.rmit.edu.au/~zen