diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-04-12 14:38:52 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-04-12 14:38:52 -0400 |
| commit | 9273cdd6387bdf2182e595bdd5144a1792e9e1a4 (patch) | |
| tree | 9913e1f2c844107064eb653787ba662d9b58a792 | |
| parent | 3d66f727f56426592d70d5cf54bbcb891172b6f0 (diff) | |
| download | sqlalchemy-9273cdd6387bdf2182e595bdd5144a1792e9e1a4.tar.gz | |
- [bug] If conn.begin() fails when calling
"with engine.begin()", the newly acquired
Connection is closed explicitly before
propagating the exception onward normally.
| -rw-r--r-- | CHANGES | 5 | ||||
| -rw-r--r-- | lib/sqlalchemy/engine/base.py | 6 | ||||
| -rw-r--r-- | test/engine/test_execute.py | 17 |
3 files changed, 27 insertions, 1 deletions
@@ -48,6 +48,11 @@ CHANGES before SQLAlchemy modifies the state of the cursor. + - [bug] If conn.begin() fails when calling + "with engine.begin()", the newly acquired + Connection is closed explicitly before + propagating the exception onward normally. + - mssql - [feature] Added interim create_engine flag supports_unicode_binds to PyODBC dialect, diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 110ac4e8a..1d2511333 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -2355,7 +2355,11 @@ class Engine(Connectable, log.Identified): """ conn = self.contextual_connect(close_with_result=close_with_result) - trans = conn.begin() + try: + trans = conn.begin() + except: + conn.close() + raise return Engine._trans_ctx(conn, trans, close_with_result) def transaction(self, callable_, *args, **kwargs): diff --git a/test/engine/test_execute.py b/test/engine/test_execute.py index 6bdbf4227..610f5e42b 100644 --- a/test/engine/test_execute.py +++ b/test/engine/test_execute.py @@ -358,6 +358,23 @@ class ConvenienceExecuteTest(fixtures.TablesTest): testing.run_as_contextmanager(ctx, fn, 5, value=8) self._assert_fn(5, value=8) + def test_transaction_engine_ctx_begin_fails(self): + engine = engines.testing_engine() + class MockConnection(Connection): + closed = False + def begin(self): + raise Exception("boom") + + def close(self): + MockConnection.closed = True + engine._connection_cls = MockConnection + fn = self._trans_fn() + assert_raises( + Exception, + engine.begin + ) + assert MockConnection.closed + def test_transaction_engine_ctx_rollback(self): fn = self._trans_rollback_fn() ctx = testing.db.begin() |
