AW: [Zope-DB] transactions
Arenz, Ralph
Ralph.Arenz@friatec.de
Thu, 16 Jan 2003 11:43:34 +0100
-----Originalnachricht-----
Von: Martin Gebert
An: Arenz, Ralph
Cc: 'zope-db@zope.org'
Gesendet: 15.01.03 00:16
Betreff: Re: [Zope-DB] transactions
Arenz, Ralph schrieb:
>Hello,
>
>i have a problem with mysql-transaction out of zope.
>
>I tried the transaction-mechanism in a external python
>program via the python DB-API , that work's fine.
>==> I used "commit" , "rollback" on the connection-object
>
>
>If i try to reproduce a transaction-handling in Zope, it fails.
>==>I tried the following in a external method and in a dtml-method:
> get_transaction().begin()
> -> call a ZSQL-Method
> get_transaction().abort()
>
>Can anyone enhance my horizon ?
>
First, in Zope the Z SQL Method and the DA shield you from calling
DB-API methods directly. The above won't work, 'cause you can't
(shouldn't!) access the DA/DB-API methods. (Sure, you could import and
use the DB-API, but don't do that except you're a Zope hacker and you
know *exactly* what you're doing; it will surely make your zopeing more
"interesting", read complicated).
Second, if you enable transaction support in your ZMySQLDA, Zope
includes the DBMS transactions into it's own TA handling where every TA
starts with a REQUEST (calling an object and interpreting it and the
invoked scripts/methods/etc.) and ends with the successful RESPONSE,
giving back the rendered output. If there's an exception somewhere
between (something that would usually result in a
standard_error_message) Zope rolls it's own *and* the DBMS transactions
back.
Ergo, you usually don't need to mess with TAs, Zope does it for you. Let
it, and you'll be happy (except for some quite weird kinds of
application...). I've made good experiences myself with this mechanism
:-)
For more knowledge about using Z SQL, please read the appropriate
chapter in the ZopeBook...
Martin
Ok,thank you,your help brought me on the wright way.
I can't use the automatic mechanism of the TM because i don't
invoke zPublisher. I have to call ZSQL-Methods from an external method
that never (except i want it) returns do Zope with a RESPONSE.
So i read the HowTo "Use ZODB Transactions" and followed the
example to obtain a transaction object. Using that Execption-Object
i will be able to control the transaction of the ZSQL-Method.
def foo(self):
try:
tmo=get_transaction()
self.ZSQLMethod()
except:
tmo.abort()
tmo.commit()
Ralph