]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- threadlocal TLConnection, when closes for real, forces parent TLSession
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 17 Aug 2007 19:13:51 +0000 (19:13 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 17 Aug 2007 19:13:51 +0000 (19:13 +0000)
to rollback/dispose of transaction

lib/sqlalchemy/engine/threadlocal.py
lib/sqlalchemy/orm/session.py
test/engine/transaction.py

index 62f402be5c6e0ececc599e0d10f01c8b61e1cafa..982be3f0529f884498dadd51f797797f0bb82a7a 100644 (file)
@@ -28,6 +28,11 @@ class TLSession(object):
             pass
         self.__tcount = 0
 
+    def _conn_closed(self):
+        if self.__tcount == 1:
+            self.__trans._trans.rollback()
+            self.reset()
+            
     def in_transaction(self):
         return self.__tcount > 0
     
@@ -104,6 +109,7 @@ class TLConnection(base.Connection):
     def close(self):
         if self.__opencount == 1:
             base.Connection.close(self)
+            self.__session._conn_closed()
         self.__opencount -= 1
 
     def _force_close(self):
index 6263a2e525a838ae63a368b63d7a0689ab2c3c34..5b39be7d0cc1465412c72c9150d32c8f3687e7fa 100644 (file)
@@ -241,7 +241,6 @@ class SessionTransaction(object):
             return
         for t in util.Set(self.__connections.values()):
             if t[2]:
-                # closing the connection will also issue a rollback()
                 t[0].close()
         self.session.transaction = None
 
index bd912a4df057030b53107f915b971fbffa64fa67..7adefbac2b2d81d06b216ce7718651d0a7293147 100644 (file)
@@ -311,6 +311,19 @@ class TLTransactionTest(PersistTest):
     def tearDownAll(self):
         users.drop(tlengine)
         tlengine.dispose()
+    
+    def testclose(self):
+        """test that when connections are closed for real, transactions are rolled back and disposed."""
+        
+        c = tlengine.contextual_connect()
+        c.begin()
+        assert tlengine.session.in_transaction()
+        assert hasattr(tlengine.session, '_TLSession__transaction')
+        assert hasattr(tlengine.session, '_TLSession__trans')
+        c.close()
+        assert not tlengine.session.in_transaction()
+        assert not hasattr(tlengine.session, '_TLSession__transaction')
+        assert not hasattr(tlengine.session, '_TLSession__trans')
         
     def testrollback(self):
         """test a basic rollback"""
@@ -343,6 +356,8 @@ class TLTransactionTest(PersistTest):
             external_connection.close()
 
     def testcommits(self):
+        assert tlengine.connect().execute("select count(1) from query_users").scalar() == 0
+        
         connection = tlengine.contextual_connect()
         transaction = connection.begin()
         connection.execute(users.insert(), user_id=1, user_name='user1')
@@ -355,7 +370,8 @@ class TLTransactionTest(PersistTest):
 
         transaction = connection.begin()
         result = connection.execute("select * from query_users")
-        assert len(result.fetchall()) == 3
+        l = result.fetchall()
+        assert len(l) == 3, "expected 3 got %d" % len(l)
         transaction.commit()
 
     def testrollback_off_conn(self):