]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added is_active flag to Sessions to detect when
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Jun 2008 20:52:11 +0000 (20:52 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Jun 2008 20:52:11 +0000 (20:52 +0000)
a transaction is in progress [ticket:976].  This
flag is always True with a "transactional"
(in 0.5 a non-"autocommit") Session.

CHANGES
lib/sqlalchemy/orm/session.py
test/orm/session.py

diff --git a/CHANGES b/CHANGES
index 377690e55d9c3a5174b49c36c65f966f7862fbf4..87a0151d1b99bad60e64b1a48510a34ba3ee0e4b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -84,6 +84,11 @@ CHANGES
       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.
index a2dbb8e1199398ff71aa91ab263b13d5382f9daa..60b88cc0e07b7a275e13e59f8c911002afc7eb5f 100644 (file)
@@ -1433,7 +1433,13 @@ class Session(object):
             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_states(self):
         """Return a set of all persistent states considered dirty.
 
index 079649aed65927a4e9121654d61fa98e98996f7b..02258a75e017417eaea7b4f738b8852a37fc65de 100644 (file)
@@ -3,7 +3,7 @@ import gc
 import inspect
 import pickle
 from sqlalchemy.orm import create_session, sessionmaker
-from testlib import engines, sa, testing
+from testlib import engines, sa, testing, config
 from testlib.sa import Table, Column, Integer, String
 from testlib.sa.orm import mapper, relation, backref
 from testlib.testing import eq_
@@ -282,6 +282,14 @@ class SessionTest(_fixtures.FixtureTest):
         session.begin()
         session.flush()
         session.commit()
+    
+    def test_active_flag(self):
+        sess = create_session(bind=config.db, autocommit=True)
+        assert not sess.is_active
+        sess.begin()
+        assert sess.is_active
+        sess.rollback()
+        assert not sess.is_active
         
     @testing.resolve_artifact_names
     def test_textual_execute(self):