Mike Bayer avatar Mike Bayer committed fe8eeed

more fixes to transaction nesting, interacts better with close() statement

Comments (0)

Files changed (3)

 out will use the default metadata.
 - fixes to session cascade behavior, entity_name propigation
 - reorganized unittests into subdirectories
+- more fixes to threadlocal connection nesting patterns
 
 0.2.1
 - "pool" argument to create_engine() properly propigates

lib/sqlalchemy/engine/threadlocal.py

         self.__tcount = 0
     def get_connection(self, close_with_result=False):
         try:
-            return self.__transaction
+            return self.__transaction._increment_connect()
         except AttributeError:
             return TLConnection(self, close_with_result=close_with_result)
     def set_transaction(self, tlconnection, trans):
     def rollback(self):
         if self.__tcount > 0:
             try:
-                self.__trans.rollback()
+                self.__trans._rollback_impl()
             finally:
                 self.reset()
     def commit(self):
         if self.__tcount == 1:
             try:
-                self.__trans.commit()
+                self.__trans._commit_impl()
             finally:
                 self.reset()
         elif self.__tcount > 1:
     def __init__(self, session, close_with_result):
         base.Connection.__init__(self, session.engine, close_with_result=close_with_result)
         self.__session = session
+        self.__opencount = 1
     session = property(lambda s:s.__session)
+    def _increment_connect(self):
+        self.__opencount += 1
+        return self
     def _create_transaction(self, parent):
         return TLTransaction(self, parent)
     def _begin(self):
         return base.Connection.begin(self)
     def begin(self):
-        trans = base.Connection.begin(self)
-        self.__session.set_transaction(self, trans)
-        return trans
-
+        return self.session.begin()
+    def close(self):
+        if self.__opencount == 1:
+            base.Connection.close(self)
+        self.__opencount -= 1
+        
 class TLTransaction(base.Transaction):
+    def _commit_impl(self):
+        base.Transaction.commit(self)
+    def _rollback_impl(self):
+        base.Transaction.rollback(self)
     def commit(self):
-        base.Transaction.commit(self)
-        if not self.is_active:
-            self.connection.session.reset()
+        self.connection.session.commit()
     def rollback(self):
-        base.Transaction.rollback(self)
-        if not self.is_active:
-            self.connection.session.reset()
+        self.connection.session.rollback()
             
 class TLEngine(base.ComposedSQLEngine):
     """a ComposedSQLEngine that includes support for thread-local managed transactions.  This engine

test/engine/transaction.py

             self.assert_(external_connection.scalar("select count(1) from query_users") == 0)
         finally:
             external_connection.close()
-    
+
+    def testsessionnesting(self):
+        class User(object):
+            pass
+        try:
+            mapper(User, users)
+
+            sess = create_session(bind_to=tlengine)
+            print "STEP1"
+            tlengine.begin()
+            print "STEP2"
+            u = User()
+            sess.save(u)
+            print "STEP3"
+            sess.flush()
+            print "STEP4"
+            tlengine.commit()
+            print "STEP5"
+        finally:
+            clear_mappers()
+
     def testconnections(self):
         """tests that contextual_connect is threadlocal"""
         c1 = tlengine.contextual_connect()
         c2 = tlengine.contextual_connect()
         assert c1.connection is c2.connection
-        c1.close()
+        c2.close()
+        assert c1.connection.connection is not None
         
 if __name__ == "__main__":
     testbase.main()        
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.