[Zope] transaction control from Zope to Postgresql? (SOLVED)

Reinoud van Leeuwen reinoud@xs4all.nl
Sun, 10 Jun 2001 15:58:53 GMT


On Fri, 8 Jun 2001 13:40:02 +0200 (CEST), I wrote:

>Hello world,
>
>I am in need for a way to explicitely contral transaction behaviour for =
a=20
>group of inserts in a postgresql database. The DTML script I use first=20
>inserts a header record and after that some detail records. The =
postgresql=20
>function that does the inserts might return an errorstring. So I tried =
the=20
>following code (simplified for readability)

Well, I found an amswer with the help of some people on irc channel
#zope (thanx Vladdrac!). Although I did not manage to get the
transaction control the way I originally planned it, I have figured
out why it did not work.
Zope rolls back the transaction automatically only when it thinks the
request went wrong. Because I had implemented a <dtml-except> clause,
Zope did not think something went really wrong.=20
I have changed the <dtml-raise> to=20

<dtml-raise type=3DRedirect><dtml-var
"error_html.absolute_url()"></dtml-raise>

(error_html is a DTML document that reports the error to the user).
Because of the redirection, Zope thinks enough went worng and rolls
back all the executed SQL for me automagically. So my problem is
solved.

><dtml-try>
>  <dtml-call "sql_to_insert_head_record (some_parameters)">
>
>  <dtml-in "a_loop_over_detail_lines">
>    <dtml-if "check_detail_line">
>      <dtml-raise required_attribute_missing> errormessage </dtml-raise>
>    </dtml-if>
>
>    <dtml-call "sql_to_insert_detail_line">
>    <dtml-if "check_returncode">
>      <dtml-raise inserterror> error inserting! </dtml-raise>
>    </dtml-if>
>  </dtml-in>
>
><dtml-except>
> insert failed (error: <dtml-var error_value>)
><dtml-else>
>  object added to database
></dtml-try>
>
>What I would like to do is that all the SQL executed between dtml-try =
and=20
>dtml-except is rolled back when the exception is raised. I tried=20
>sending "begin" and "rollback" to the database, but that does not work=20
>because Zope sends some transaction control by itself.
>I tried getting get_transaction().begin() and get_transaction().abort() =
to=20
>work, but I cannot reach that function from dtml not python script.=20
>Anyone has some clues to spare?
>
>Have a nice weekend,
>Reinoud

--=20
__________________________________________________
"Nothing is as subjective as reality"
Reinoud van Leeuwen       reinoud@xs4all.nl
http://www.xs4all.nl/~reinoud
__________________________________________________