Skip to content

sessiontransaction contextmanager is broken due to new states in _assert_active() #2718

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)

e = create_engine("postgresql://scott:tiger@localhost/test")

Base.metadata.drop_all(e)
Base.metadata.create_all(e)

s = Session(e)

s.add(A(id=1))
s.commit()

try:
    with s.begin_nested():
        s.add(A(id=1))
        s.flush()
except Exception, e:
    print e

with s.begin_nested():
    s.add(A(id=2))
    s.flush()

assert s.query(A).count() == 2

patch:

diff -r 62eeb1bf26c9599f8a3c8eeeeedfbb40072bd9d8 lib/sqlalchemy/orm/session.py
--- a/lib/sqlalchemy/orm/session.py	Tue Apr 30 00:15:36 2013 -0400
+++ b/lib/sqlalchemy/orm/session.py	Tue Apr 30 09:48:35 2013 -0400
@@ -169,6 +169,7 @@
 
     def _assert_active(self, prepared_ok=False,
                         rollback_ok=False,
+                        deactive_ok=False,
                         closed_msg="This transaction is closed"):
         if self._state is COMMITTED:
             raise sa_exc.InvalidRequestError(
@@ -182,7 +183,7 @@
                         "SQL can be emitted within this transaction."
                     )
         elif self._state is DEACTIVE:
-            if not rollback_ok:
+            if not deactive_ok and not rollback_ok:
                 if self._rollback_exception:
                     raise sa_exc.InvalidRequestError(
                         "This Session's transaction has been rolled back "
@@ -192,7 +193,7 @@
                         " Original exception was: %s"
                         % self._rollback_exception
                     )
-                else:
+                elif not deactive_ok:
                     raise sa_exc.InvalidRequestError(
                         "This Session's transaction has been rolled back "
                         "by a nested rollback() call.  To begin a new "
@@ -435,7 +436,7 @@
         return self
 
     def __exit__(self, type, value, traceback):
-        self._assert_active(prepared_ok=True)
+        self._assert_active(deactive_ok=True, prepared_ok=True)
         if self.session.transaction is None:
             return
         if type is None:

Activity

sqlalchemy-bot

sqlalchemy-bot commented on Apr 30, 2013

@sqlalchemy-bot
CollaboratorAuthor

Michael Bayer (@zzzeek) wrote:

0c4f0f8

sqlalchemy-bot

sqlalchemy-bot commented on Apr 30, 2013

@sqlalchemy-bot
CollaboratorAuthor

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
added
bugSomething isn't working
blockerissue that must be resolved asap as it is preventing things from working
on Nov 27, 2018
added this to the 0.8.xx milestone on Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockerissue that must be resolved asap as it is preventing things from workingbugSomething isn't workingorm

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sqlalchemy-bot

        Issue actions

          sessiontransaction contextmanager is broken due to new states in _assert_active() · Issue #2718 · sqlalchemy/sqlalchemy