Commits

Mike Bayer committed 0298783

merged r4861, session.is_active, from trunk

  • Participants
  • Parent commits 2b48615
  • Branches rel_0_4

Comments (0)

Files changed (3)

       mapper inheritance against the target mapper are 
       still not allowed.
 
+    - Added is_active flag to Sessions to detect when 
+      a transaction is in progress [ticket:976].  This
+      flag is always True with a "transactional" 
+      (in 0.5 a non-"autocommit") Session.
+    
 - postgres
     - Repaired server_side_cursors to properly detect 
       text() clauses.

lib/sqlalchemy/orm/session.py

             if added or deleted:
                 return True
         return False
-
+    
+    def is_active(self):
+        """return True if this Session has an active transaction."""
+        
+        return self.transaction and self.transaction.is_active
+    is_active = property(is_active)
+    
     def dirty(self):
         """Return a ``Set`` of all instances marked as 'dirty' within this ``Session``.
 

test/orm/session.py

 from sqlalchemy.orm.session import Session as SessionCls
 from testlib import *
 from testlib.tables import *
-from testlib import fixtures, tables
+from testlib import fixtures, tables, config
 import pickle
 import gc
 
         assert len(u.addresses) == 3
         assert newad not in u.addresses
 
+    def test_active_flag(self):
+        sess = create_session(bind=config.db, transactional=False)
+        assert not sess.is_active
+        sess.begin()
+        assert sess.is_active
+        sess.rollback()
+        assert not sess.is_active
 
     @engines.close_open_connections
     def test_external_joined_transaction(self):