From: Mike Bayer Date: Thu, 17 Aug 2006 18:04:13 +0000 (+0000) Subject: commit should be outside of the try/except; else when commit fails, rollback gets... X-Git-Tag: rel_0_2_8~46 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=819837884e279be306a63e7f6640618afc236e6d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git commit should be outside of the try/except; else when commit fails, rollback gets called which is technically invalid (although SA transaction probably lets it slide...this should also possibly be made more strict) --- diff --git a/doc/build/content/unitofwork.txt b/doc/build/content/unitofwork.txt index 0adc382f1e..8319ecd09f 100644 --- a/doc/build/content/unitofwork.txt +++ b/doc/build/content/unitofwork.txt @@ -369,10 +369,10 @@ Example usage is as follows: item2 = sess.query(Item).get(2) item1.foo = 'bar' item2.bar = 'foo' - trans.commit() except: trans.rollback() raise + trans.commit() The `create_transaction()` method creates a new SessionTransaction object but does not declare any connection/transaction resources. At the point of the first `get()` call, a connection resource is opened off the engine that corresponds to the Item classes' mapper and is stored within the `SessionTransaction` with an open `Transaction`. When `trans.commit()` is called, the `flush()` method is called on the `Session` and the corresponding update statements are issued to the database within the scope of the transaction already opened; afterwards, the underying Transaction is committed, and connection resources are freed. @@ -393,10 +393,10 @@ To associate a specific `Connection` with the `SessionTransaction`, use the `add trans.add(connection) connection.execute(mytable.update(), {'col1':4, 'col2':17}) session.flush() # flush() operation will use the same connection - trans.commit() except: trans.rollback() raise + trans.commit() The `add()` method will key the `Connection`'s underlying `Engine` to this `SessionTransaction`. When mapper operations are performed against this `Engine`, the `Connection` explicitly added will be used. This **overrides** any other `Connection` objects that the underlying Session was associated with, corresponding to the underlying `Engine` of that `Connection`. However, if the `SessionTransaction` itself is already associated with a `Connection`, then an exception is thrown. @@ -407,10 +407,10 @@ The other way is just to use the `Connection` referenced by the `SessionTransact try: connection = trans.connection(UserClass) # get the Connection used by the UserClass' Mapper connection.execute(mytable.update(), {'col1':4, 'col2':17}) - trans.commit() except: trans.rollback() raise + trans.commit() The `connection()` method also exists on the `Session` object itself, and can be called regardless of whether or not a `SessionTransaction` is in progress. If a `SessionTransaction` is in progress, it will return the connection referenced by the transaction. If an `Engine` is being used with `threadlocal` strategy, the `Connection` returned will correspond to the connection resources that are bound to the current thread, if any (i.e. it is obtained by calling `contextual_connection()`). @@ -427,10 +427,10 @@ The transactions issued by `SessionTransaction` as well as internally by the `Se stuff[2].foo = 'bar' connection.execute(mytable.insert(), dict(id=12, value="bar")) # use connection explicitly session.flush() # Session flushes with "connection", using transaction "trans" - trans.commit() # commit except: trans.rollback() # or rollback raise + trans.commit() # commit ### Analyzing Object Flushes {@name=logging} diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index ce2950d58a..33e0149e45 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -195,10 +195,10 @@ class UnitOfWork(object): flush_context.transaction = trans try: flush_context.execute(echo=echo) - trans.commit() except: trans.rollback() raise + trans.commit() flush_context.post_exec()