Commits

Mike Bayer committed 47d4bc4

TLConnection insures that it is used to create a transaction via the session when begin() is called, so that it has proper transactional context, + unittests

  • Participants
  • Parent commits e8d9f08

Comments (0)

Files changed (3)

 0.2.4
 - try/except when the mapper sets init.__name__ on a mapped class,
 supports python 2.3
+- fixed bug where threadlocal engine would still autocommit
+despite a transaction in progress
 - lazy load and deferred load operations require the parent object
 to be in a Session to do the operation; whereas before the operation
 would just return a blank list or None, it now raises an exception.

lib/sqlalchemy/engine/threadlocal.py

         except AttributeError:
             pass
         self.__tcount = 0
-        
-    def begin(self):
+    def begin(self, tlconnection=None):
         if self.__tcount == 0:
-            self.__transaction = self.get_connection()
+            if tlconnection is None:
+                self.__transaction = self.get_connection()
+            else:
+                self.__transaction = tlconnection
             self.__trans = self.__transaction._begin()
         self.__tcount += 1
         return self.__trans
     def _begin(self):
         return base.Connection.begin(self)
     def begin(self):
-        return self.session.begin()
+        return self.session.begin(self)
     def close(self):
         if self.__opencount == 1:
             base.Connection.close(self)

test/engine/transaction.py

         finally:
             external_connection.close()
 
+    @testbase.unsupported('mysql')
+    def testrollback_off_conn(self):
+        conn = tlengine.contextual_connect()
+        trans = conn.begin()
+        conn.execute(users.insert(), user_id=1, user_name='user1')
+        conn.execute(users.insert(), user_id=2, user_name='user2')
+        conn.execute(users.insert(), user_id=3, user_name='user3')
+        trans.rollback()
+
+        external_connection = tlengine.connect()
+        result = external_connection.execute("select * from query_users")
+        try:
+            assert len(result.fetchall()) == 0
+        finally:
+            external_connection.close()
+
+    @testbase.unsupported('mysql')
+    def testcommit_off_conn(self):
+        conn = tlengine.contextual_connect()
+        trans = conn.begin()
+        conn.execute(users.insert(), user_id=1, user_name='user1')
+        conn.execute(users.insert(), user_id=2, user_name='user2')
+        conn.execute(users.insert(), user_id=3, user_name='user3')
+        trans.commit()
+
+        external_connection = tlengine.connect()
+        result = external_connection.execute("select * from query_users")
+        try:
+            assert len(result.fetchall()) == 3
+        finally:
+            external_connection.close()
+        
     @testbase.unsupported('mysql', 'sqlite')
     def testnesting(self):
         """tests nesting of tranacstions"""