]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] If conn.begin() fails when calling
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Apr 2012 18:38:52 +0000 (14:38 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 12 Apr 2012 18:38:52 +0000 (14:38 -0400)
"with engine.begin()", the newly acquired
Connection is closed explicitly before
propagating the exception onward normally.

CHANGES
lib/sqlalchemy/engine/base.py
test/engine/test_execute.py

diff --git a/CHANGES b/CHANGES
index 96383e1d74be6bb193a9358f7be09a5057aef7bb..633695d8d3155ffcaaf3606ae3fea7b213123dc2 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -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,
index 110ac4e8a173503bb3c8acad027d9290526ba0b4..1d251133379b8c03f2458e42c1c4ef044857334a 100644 (file)
@@ -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):
index 6bdbf422724f99fc205e5858022f178858ad5681..610f5e42b0b88d50ce305a92cde3962a14c7bfdb 100644 (file)
@@ -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()