From: Ian Bicking <ianb@co...> - 2005-10-31 16:53:00
Jeff Watkins wrote:
> I'm not certain whether the problem I'm about to describe is in
> SQLObject or in TurboGears. So I've sent this email to both lists.
> I have the following model object:
> hub = PackageHub("cms")
> __connection__ = hub
> class Article(SQLObject,ModelHelper):
> slug= StringCol( alternateID=True, length=255 )
> sourceFile= StringCol( alternateID=True )
> # more stuff
> I've created a single Article so far. When I load a fresh tg-admin
> shell, and attempt to create a new transaction to manipulate the
> article, the following happens:
> Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from cms.model import *
> >>> a= Article.get(1)
> >>> a._connection.transaction().begin()
> Traceback (most recent call last):
> File "<console>", line 1, in ?
> File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
> dbconnection.py", line 809, in begin
> assert self._obsolete, "You cannot begin a new transaction session
> without rolling back this one"
> AssertionError: You cannot begin a new transaction session without
> rolling back this one
> You may well ask why I'm going through the weird hoops to get a
> transaction. The answer is I'm working on a generic function which
> needs to create a transaction when modifying a model object. The above
> is just an example of me trying to puzzle out how SQLObject works.
> Can anyone explain to me how a transaction was begun in the snippet above?
Calling .transaction() gives you a transaction connection, that has to
later be used to be of any use. It's already begun, only if you
rollback or commit must you call .begin().
Here's the routine I generally use for transactions:
def do_in_transaction(func, *args, **kw):
old_conn = sqlhub.getConnection()
conn = old_conn.transaction()
sqlhub.threadConnection = conn
value = func(*args, **kw)
sqlhub.threadConnection = old_conn
This might be a useful addition to the expose decorator. Actually, I
could probably attach the function to ConnectionHub too -- it ought to
exist somewhere in SQLObject.
Ian Bicking / ianb@... / http://blog.ianbicking.org