]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- session transaction closing tweak for threadlocal
authorJason Kirtland <jek@discorporate.us>
Fri, 17 Aug 2007 15:01:20 +0000 (15:01 +0000)
committerJason Kirtland <jek@discorporate.us>
Fri, 17 Aug 2007 15:01:20 +0000 (15:01 +0000)
- connection-rollback decorator only fires on unhandled testing exceptions

lib/sqlalchemy/orm/session.py
test/orm/session.py
test/testlib/engines.py

index 6263a2e525a838ae63a368b63d7a0689ab2c3c34..109c468fc84709d3ecd8d277449881f675f8124a 100644 (file)
@@ -241,7 +241,9 @@ class SessionTransaction(object):
             return
         for t in util.Set(self.__connections.values()):
             if t[2]:
+                # fixme: wrong-
                 # closing the connection will also issue a rollback()
+                t[1].rollback()
                 t[0].close()
         self.session.transaction = None
 
index 337acd51daa53922931d28119136d24b28d1339c..6a8e9dfe506363fab96242a5413bbdca0c340234 100644 (file)
@@ -79,6 +79,7 @@ class SessionTest(AssertMixin):
         # then see if expunge fails
         session.expunge(u)
     
+    @engines.rollback_open_connections
     def test_binds_from_expression(self):
         """test that Session can extract Table objects from ClauseElements and match them to tables."""
         Session = sessionmaker(binds={users:testbase.db, addresses:testbase.db})
@@ -93,9 +94,10 @@ class SessionTest(AssertMixin):
         Session = sessionmaker(binds={User:testbase.db, Address:testbase.db})
         sess.execute(users.insert(), params=dict(user_id=2, user_name='fred'))
         assert sess.execute(users.select()).fetchall() == [(1, 'ed'), (2, 'fred')]
-        
+        sess.close()
         
     @testing.unsupported('sqlite', 'mssql') # TEMP: test causes mssql to hang
+    @engines.rollback_open_connections
     def test_transaction(self):
         class User(object):pass
         mapper(User, users)
@@ -111,8 +113,10 @@ class SessionTest(AssertMixin):
         sess.commit()
         assert conn1.execute("select count(1) from users").scalar() == 1
         assert testbase.db.connect().execute("select count(1) from users").scalar() == 1
+        sess.close()
     
     @testing.unsupported('sqlite', 'mssql') # TEMP: test causes mssql to hang
+    @engines.rollback_open_connections
     def test_autoflush(self):
         class User(object):pass
         mapper(User, users)
@@ -130,8 +134,10 @@ class SessionTest(AssertMixin):
         sess.commit()
         assert conn1.execute("select count(1) from users").scalar() == 1
         assert testbase.db.connect().execute("select count(1) from users").scalar() == 1
+        sess.close()
 
     @testing.unsupported('sqlite', 'mssql') # TEMP: test causes mssql to hang
+    @engines.rollback_open_connections
     def test_autoflush_unbound(self):
         class User(object):pass
         mapper(User, users)
@@ -148,10 +154,12 @@ class SessionTest(AssertMixin):
             sess.commit()
             assert sess.execute("select count(1) from users", mapper=User).scalar() == 1
             assert testbase.db.connect().execute("select count(1) from users").scalar() == 1
+            sess.close()
         except:
             sess.rollback()
             raise
             
+    @engines.rollback_open_connections
     def test_autoflush_2(self):
         class User(object):pass
         mapper(User, users)
@@ -165,6 +173,7 @@ class SessionTest(AssertMixin):
         sess.commit()
         assert conn1.execute("select count(1) from users").scalar() == 1
         assert testbase.db.connect().execute("select count(1) from users").scalar() == 1
+        sess.commit()
 
     # TODO: not doing rollback of attributes right now.
     def dont_test_autoflush_rollback(self):
@@ -189,6 +198,7 @@ class SessionTest(AssertMixin):
         assert newad not in u.addresses
         
         
+    @engines.rollback_open_connections
     def test_external_joined_transaction(self):
         class User(object):pass
         mapper(User, users)
@@ -202,8 +212,10 @@ class SessionTest(AssertMixin):
         sess.commit() # commit does nothing
         trans.rollback() # rolls back
         assert len(sess.query(User).select()) == 0
+        sess.close()
 
     @testing.supported('postgres', 'mysql')
+    @engines.rollback_open_connections
     def test_external_nested_transaction(self):
         class User(object):pass
         mapper(User, users)
@@ -263,6 +275,7 @@ class SessionTest(AssertMixin):
         sess.commit() # commit does nothing
         sess.rollback() # rolls back
         assert len(sess.query(User).select()) == 0
+        sess.close()
 
     @testing.supported('postgres', 'mysql')
     @testing.exclude('mysql', '<', (5, 0, 3))
@@ -286,6 +299,7 @@ class SessionTest(AssertMixin):
     
         sess.commit()
         assert len(sess.query(User).select()) == 1
+        sess.close()
 
     @testing.supported('postgres', 'mysql')
     @testing.exclude('mysql', '<', (5, 0, 3))
@@ -302,12 +316,14 @@ class SessionTest(AssertMixin):
         u2 = User()
         sess.save(u2)
         sess.flush()
-
+        
         sess.rollback() 
 
         sess.commit()
         assert len(sess.query(User).select()) == 1
+        sess.close()
 
+    @engines.rollback_open_connections
     def test_bound_connection(self):
         class User(object):pass
         mapper(User, users)
@@ -340,6 +356,7 @@ class SessionTest(AssertMixin):
             
         transaction.rollback()
         assert len(sess.query(User).select()) == 0
+        sess.close()
              
     def test_update(self):
         """test that the update() method functions and doesnet blow away changes"""
index a66b336e948fa8ac4b4f7878cc24bc222c5b3c1b..414d262dea2a6a635a1a2d0b8d1f8c8ba6299382 100644 (file)
@@ -21,12 +21,12 @@ class ConnectionKiller(object):
                 except Exception, e:
                     # fixme
                     sys.stderr.write("\n" + str(e) + "\n")
-        del self.record_refs[:]
 
     def rollback_all(self):
         self._apply_all(('rollback',))
+
     def close_all(self):
-        self._apply_all(('rollback','close'))
+        self._apply_all(('rollback', 'close'))
 
 testing_reaper = ConnectionKiller()
 
@@ -36,8 +36,9 @@ def rollback_open_connections(fn):
     def decorated(*args, **kw):
         try:
             fn(*args, **kw)
-        finally:
+        except:
             testing_reaper.rollback_all()
+            raise
     decorated.__name__ = fn.__name__
     return decorated