[Zope] q: How should I get a guaranteed unique id in Zope?

Jason Earl jason.earl@simplot.com
15 Jan 2002 15:09:10 -0700


Chris Kratz <chris.kratz@vistashare.com> writes:

> On Tuesday 15 January 2002 01:28 pm, Chris Meyers wrote:

> > This works really well, but could cause problems if the db is
> > heavily used.  Another transaction could sneak in between your
> > INSERT and the SELECT and throw off your count (unless Zope wraps
> > ZSQL Methods in a transaction block which I am not sure
> > about). You could do something like this if you want to be really
> > sure that no new ids are slipped in:
> 
> We've tested this pretty extensively in our situation and with
> Psycopg the entire request is in a single transaction so there is no
> problem with another transaction "sneaking in".  Zope handles this
> automatically for you with psycopg and postgres as long as you don't
> try to do the transactional work yourself.  You can verify this by
> turning on logging in postgres and watching the logs.
> 
> -Chris

I am glad that I am not the only one that has notice this "correct"
behavior.  I especially like the fact that Zope will happily roll back
PostgreSQL transactions on failure.  That makes combining Zope and
PostgreSQL so much easier.

Of course asking for a unique id from the sequence in one ZSQL method
(or Python Script or whatever) with nextval and then inserting it
works fine too.  Using currval simply saves a step, you only have to
write the one ZSQL method (Hey, I'm lazy).