On 5 Nov, 2005, at 1:06 pm, DaveS wrote:
I'm trying to figure out how to structure my code so transactions do
the right thing when exceptions are raised. So far I've got
def update(self):
hub.begin()
self.modify_database()
if i_dont_like_things:
hub.rollback()
else:
hub.commit()
hub.end()
Is there any way to have a rollback happend automatically if
modify_database raises an exception? Or do I need to wrap everything
in a try/except to guarantee a rollback (and I assume I would also
need
a hub.end() in there as well).
You might consider writing your code like this:
def update(self):
try:
hub.begin()
# do some stuff
hub.commit()
finally:
hub.rollback()
From what I've seen, rolling back a transaction after committing
won't cause problems. This way, no matter how your code exits the
block, the transaction will be committed or rolled back. This doesn't
force you to catch any exceptions.
--
Jeff Watkins
http://newburyportion.com/
In the USDA study [of the meat packing industry conducted in 1996]
78.6 percent of the ground beef contained microbes that are spread
primarily by fecal material. The medical literature on the causes of
food poisoning is full of euphemisms and dry scientific terms:
coliform levels, aerobic plate counts, sorbitol, MacConkey agar, and
so on. Behind them lies a simple explanation for why eating a
hamburger can now make you seriously ill: There is shit in the meat.
-- Eric Schlosser, Fast Food Nation