Tuesday, February 28, 2006

sqlobject transactions

When trying to figure out how to use transactions in sqlobject, I found this code snippet.

As a bonus my init database code now runs twice as fast. Yah!


def do_in_transaction(func, *args, **kw):
old_conn = sqlhub.getConnection()
conn = old_conn.transaction()
sqlhub.threadConnection = conn
try:
try:
value = func(*args, **kw)
except:
conn.rollback()
raise
else:
conn.commit()
return value
finally:
sqlhub.threadConnection = old_conn

# do_in_transaction(some_function_with_sqlobject_stuff_in_it)

# I needed to change my connection line to this. That is assign something
# to the processConnection thing in sqlhub.
# sqlhub.processConnection = db_connection = __connection__ = connectionForURI(config.conn)

No comments: