Does MySQLdb rollback on control-C? Maybe not.
Mike Kent
mrmakent at cox.net
Fri Sep 3 15:41:39 EDT 2010
On Sep 3, 12:22 am, John Nagle <na... at animats.com> wrote:
> I would expect MySQLdb to rollback on a control-C, but it doesn't
> seem to have done so.
> Something is broken.
I wouldn't expect it to, I'd expect to roll back on an exception, or
commit if not. Perhaps this will help you. I use it in production
code.
##
# This is a transaction context manager. It will ensure that the code
in
# the context block will be executed inside a transaction. If any
exception
# occurs, the transaction will be rolled back, and the exception
reraised.
# If no exception occurs, the transaction will be committed.
# db is a database connection object.
from contextlib import contextmanager
@contextmanager
def transaction(db):
db.begin()
try:
yield None
except:
db.rollback()
raise
else:
db.commit()
More information about the Python-list
mailing list