]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed session.transaction.commit() on a autocommit=False session not starting a new...
authorAnts Aasma <ants.aasma@gmail.com>
Tue, 30 Sep 2008 09:24:27 +0000 (09:24 +0000)
committerAnts Aasma <ants.aasma@gmail.com>
Tue, 30 Sep 2008 09:24:27 +0000 (09:24 +0000)
Moved starting a new transaction in case of previous closing into SessionTransaction.

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

diff --git a/CHANGES b/CHANGES
index 3f981b927df588a9baa8ec46c00a9e2864c9eeed..6ce7d07c7afac50d471fbf229ada70ac8efccd11 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -40,6 +40,9 @@ CHANGES
     - Removed the "raiseerror" keyword argument from object_mapper()
       and class_mapper().  These functions raise in all cases
       if the given class/instance is not mapped.
+
+    - Fixed session.transaction.commit() on a autocommit=False
+      session not starting a new transaction.
 - sql
     - column.in_(someselect) can now be used as a columns-clause
       expression without the subquery bleeding into the FROM clause
index 0ca59141e069993870c408aa6779ca00bc91b8a7..cbff0ef0d1675c8491d0d0d187af1e003ea9bbaf 100644 (file)
@@ -428,6 +428,8 @@ class SessionTransaction(object):
                     connection.close()
                 else:
                     transaction.close()
+            if not self.session.autocommit:
+                self.session.begin()
         self._deactivate()
         self.session = None
         self._connections = None
@@ -641,8 +643,6 @@ class Session(object):
             pass
         else:
             self.transaction.rollback()
-        if self.transaction is None and not self.autocommit:
-            self.begin()
 
     def commit(self):
         """Flush pending changes and commit the current transaction.
@@ -667,8 +667,6 @@ class Session(object):
                 raise sa_exc.InvalidRequestError("No transaction is begun.")
 
         self.transaction.commit()
-        if self.transaction is None and not self.autocommit:
-            self.begin()
 
     def prepare(self):
         """Prepare the current transaction in progress for two phase commit.
@@ -772,9 +770,6 @@ class Session(object):
         if self.transaction is not None:
             for transaction in self.transaction._iterate_parents():
                 transaction.close()
-        if not self.autocommit:
-            # note this doesnt use any connection resources
-            self.begin()
 
     def close_all(cls):
         """Close *all* sessions in memory."""
index f6cd2cc91a3129354bc7a98fcdd5f73df21113a9..4597137939149bc697e162f7bf6beb2406c8eb72 100644 (file)
@@ -572,6 +572,15 @@ class SessionTest(_fixtures.FixtureTest):
         self.assertRaisesMessage(sa.exc.InvalidRequestError, "inactive due to a rollback in a subtransaction", sess.begin, subtransactions=True)
         sess.close()
 
+    @testing.resolve_artifact_names
+    def test_no_autocommit_with_explicit_commit(self):
+        mapper(User, users)
+        session = create_session(autocommit=False)
+
+        session.add(User(name='ed'))
+        session.transaction.commit()
+        assert session.transaction is not None, "autocommit=False should start a new transaction"
+
     @engines.close_open_connections
     @testing.resolve_artifact_names
     def test_bound_connection(self):