From: Mike Bayer Date: Tue, 17 Jun 2008 20:55:05 +0000 (+0000) Subject: merged r4861, session.is_active, from trunk X-Git-Tag: rel_0_4_7~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b5261fa1fb338efbbf83fb600516f183de247a58;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git merged r4861, session.is_active, from trunk --- diff --git a/CHANGES b/CHANGES index 3e53e00bce..e6572d368e 100644 --- a/CHANGES +++ b/CHANGES @@ -22,6 +22,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. diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 072db43921..3dfe0411c3 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -1173,7 +1173,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(self): """Return a ``Set`` of all instances marked as 'dirty' within this ``Session``. diff --git a/test/orm/session.py b/test/orm/session.py index 49932f8d9d..eae0eab112 100644 --- a/test/orm/session.py +++ b/test/orm/session.py @@ -6,7 +6,7 @@ from sqlalchemy.orm.session import SessionExtension 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 @@ -245,6 +245,13 @@ class SessionTest(TestBase, AssertsExecutionResults): 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):