[Zope-DB] How to use commit/rollback etc. with Zope +ZpsycopgDA ???

Emmanuel Courcelle emmanuel.courcelle@toulouse.inra.fr
Tue, 26 Mar 2002 12:21:18 +0100


Federico Di Gregorio wrote:
> 
> Il lun, 2002-03-25 alle 15:44, Emmanuel Courcelle ha scritto:
> [snip]
> > The pb is that if one of the context.zsql_some_other_method(...)  gets
> > an error,
> > the database is inconsistent. This could be solved using only ONE
> > transaction,
> > BUT the Zope zsql methods are wrapped inside a transaction, so that
> > I have a transaction for EACH zsql call.
> 
> this looks strange. i always tought (and it always worked that way) that
> zope only execute a ._finish() on the connection just before sending the
> data back (i.e., just one transaction no matter how many zsql methods
> you call.) what version of psycopg are you using? not 1.1pre2 i hope,
> transaction management is seriously broken in the 1.1 series.
> 
>

OK

Thanks for your answer, the pb is that I did not correctly manage the
exception.
I am currently using psycpg 1.0.6, it works perfectly well for me.

Here is the solution I found (is there a better solution ?):

Instead of just writing:

#####
context.zsql_some_zsql_method(...)
for i in some_list:
    context.zsql_some_other_zsql_method(...)
#####

I did write:

#####
t = get_transaction()
try:
   context.zsql_some_zsql_method(...)
   for i in some_list:
       context.zsql_some_other_zsql_method(...)
except:
   t.abort()
   raise
#####

And everything seems to be OK now.
Thanks for your help !!!
Salut.

-- 
Emmanuel COURCELLE                   emmanuel.courcelle@toulouse.inra.fr
L.B.M.R.P.M. (C.N.R.S.-I.N.R.A.)     tel (33) 5-61-28-54-50
B.P.27 - 31326 CASTANET TOLOSAN Cedex
------
Pourquoi, j'voudrais savoir pourquoi, pourquoi, c'est les oiseaux
jamais les balles, qu'on arrete en plein vol ?
http://www.kulichki.com/vv/