Mike Bayer avatar Mike Bayer committed 0298783

merged r4861, session.is_active, from trunk

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):
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.