transaction control from Zope to Postgresql?
Hello world, I am in need for a way to explicitely contral transaction behaviour for a group of inserts in a postgresql database. The DTML script I use first inserts a header record and after that some detail records. The postgresql function that does the inserts might return an errorstring. So I tried the following code (simplified for readability) <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 dtml-except is rolled back when the exception is raised. I tried sending "begin" and "rollback" to the database, but that does not work because Zope sends some transaction control by itself. I tried getting get_transaction().begin() and get_transaction().abort() to work, but I cannot reach that function from dtml not python script. Anyone has some clues to spare? Have a nice weekend, Reinoud
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 group of inserts in a postgresql database. The DTML script I use first inserts a header record and after that some detail records. The postgresql function that does the inserts might return an errorstring. So I tried the 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. I have changed the <dtml-raise> to <dtml-raise type=Redirect><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 dtml-except is rolled back when the exception is raised. I tried sending "begin" and "rollback" to the database, but that does not work because Zope sends some transaction control by itself. I tried getting get_transaction().begin() and get_transaction().abort() to work, but I cannot reach that function from dtml not python script. Anyone has some clues to spare?
Have a nice weekend, Reinoud
-- __________________________________________________ "Nothing is as subjective as reality" Reinoud van Leeuwen reinoud@xs4all.nl http://www.xs4all.nl/~reinoud __________________________________________________
participants (2)
-
Reinoud van Leeuwen -
reinoud@xs4all.nl