Hello. I'm trying to wrap a function call in a transaction, but when I
intentionally throw an exception in the middle of the function it
doesn't actually roll back the transaction. The debug output says
1/ROLLBACK, without any 1/COMMITs in there, but when I view the data in
the command-line mysql utility the changes have been made.
This is the code I'm using to connect to the mysql database and to wrap
the function call in a transaction. After that I've invluded the
testUpdate method I'm using, and after that the python conversation
that ensued. Does anyone see what I'm doing wrong?
--- sqlutil.py:
from sqlobject import *
def connect():
""" Connects SQLObject to the dev database on localhost.
"""
connectionStrin g =
"mysql://admin@localhost/mc_image_librar y_dev?debug=1"
connection = connectionForUR I (connectionStri ng)
sqlhub.processC onnection = connection
def wrapInTransacti on (func, *args, **kw):
""" Got this from the SQLObject mailing list.
Calls the given func with the given args and keyword assignments
within a db transaction. Rolls back if an exception is thrown,
otherwise commits.
"""
old_conn = sqlhub.getConne ction()
conn = old_conn.transa ction()
sqlhub.processC onnection = conn
try:
try:
value = func(*args, **kw)
except:
conn.rollback()
raise
else:
conn.commit()
return value
finally:
sqlhub.processC onnection = old_conn
------------------
----- test.py:
from ImageCategory import *
def testUpdate (newName, username, fail):
category = ImageCategory.g et(1)
category.name = newName
category.update LastChanged (username)
if fail:
raise Exception ('spam', 'eggs')
-----------------
------ The python conversation:
[color=blue][color=green][color=darkred]
>>> import sqlutil
>>> sqlutil.connect ()
>>> import test
>>> sqlutil.wrapInT ransaction (test.testUpdat e, 'Animals', 'jake', True)[/color][/color][/color]
1/QueryOne: SELECT last_changed_by , last_changed_da te, name FROM
image_category WHERE id = 1
1/Query : UPDATE image_category SET name = 'Animals' WHERE id = 1
1/Query : UPDATE image_category SET last_changed_by = 'jake' WHERE
id = 1
1/Query : UPDATE image_category SET last_changed_da te = '2005-11-29
00:36:22' WHERE id = 1
1/ROLLBACK:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "sqlutil.py ", line 22, in wrapInTransacti on
value = func(*args, **kw)
File "test.py", line 8, in testUpdate
raise Exception ('spam', 'eggs')
Exception: ('spam', 'eggs')
------------
After all this, the mysql utility shows that the update did take
effect.
Any thoughts?
- Jake
intentionally throw an exception in the middle of the function it
doesn't actually roll back the transaction. The debug output says
1/ROLLBACK, without any 1/COMMITs in there, but when I view the data in
the command-line mysql utility the changes have been made.
This is the code I'm using to connect to the mysql database and to wrap
the function call in a transaction. After that I've invluded the
testUpdate method I'm using, and after that the python conversation
that ensued. Does anyone see what I'm doing wrong?
--- sqlutil.py:
from sqlobject import *
def connect():
""" Connects SQLObject to the dev database on localhost.
"""
connectionStrin g =
"mysql://admin@localhost/mc_image_librar y_dev?debug=1"
connection = connectionForUR I (connectionStri ng)
sqlhub.processC onnection = connection
def wrapInTransacti on (func, *args, **kw):
""" Got this from the SQLObject mailing list.
Calls the given func with the given args and keyword assignments
within a db transaction. Rolls back if an exception is thrown,
otherwise commits.
"""
old_conn = sqlhub.getConne ction()
conn = old_conn.transa ction()
sqlhub.processC onnection = conn
try:
try:
value = func(*args, **kw)
except:
conn.rollback()
raise
else:
conn.commit()
return value
finally:
sqlhub.processC onnection = old_conn
------------------
----- test.py:
from ImageCategory import *
def testUpdate (newName, username, fail):
category = ImageCategory.g et(1)
category.name = newName
category.update LastChanged (username)
if fail:
raise Exception ('spam', 'eggs')
-----------------
------ The python conversation:
[color=blue][color=green][color=darkred]
>>> import sqlutil
>>> sqlutil.connect ()
>>> import test
>>> sqlutil.wrapInT ransaction (test.testUpdat e, 'Animals', 'jake', True)[/color][/color][/color]
1/QueryOne: SELECT last_changed_by , last_changed_da te, name FROM
image_category WHERE id = 1
1/Query : UPDATE image_category SET name = 'Animals' WHERE id = 1
1/Query : UPDATE image_category SET last_changed_by = 'jake' WHERE
id = 1
1/Query : UPDATE image_category SET last_changed_da te = '2005-11-29
00:36:22' WHERE id = 1
1/ROLLBACK:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "sqlutil.py ", line 22, in wrapInTransacti on
value = func(*args, **kw)
File "test.py", line 8, in testUpdate
raise Exception ('spam', 'eggs')
Exception: ('spam', 'eggs')
------------
After all this, the mysql utility shows that the update did take
effect.
Any thoughts?
- Jake
Comment