]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Update "transaction has already begun" language
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Nov 2021 17:36:43 +0000 (13:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 4 Nov 2021 17:40:22 +0000 (13:40 -0400)
As future connections will now be autobeginning, there
will be more cases where begin() can't be called as well as where isolation level
can't be set, which will be surprising as this is a behavioral
change for 2.0; additionally, when DBAPI autocommit is set,
there isn't actually a DBAPI level transaction in effect even though
Connection has a Transaction object.  Clarify the language in these
two error messages to make it clear that begin() and autobegin
are tracking a SQLAlchemy-level Transaction() object, whether or not
the DBAPI has actually started a transaction, and that this is the
reason rollback() or commit() is required before performing
the requsted operation.   Additionally make sure the error message
mentions "autobegin" as a likely reason this error is being
encountered along with what Connection needs the user to do in
order to resolve.

Change-Id: If8763939eeabc46aa9d9209a56d05ad82b892c5c
(cherry picked from commit fee0855bfe2982927ab21ce7398fa48b90af7ca4)

lib/sqlalchemy/engine/base.py
lib/sqlalchemy/engine/default.py
test/engine/test_transaction.py

index 2444b5c7fe1a3d351d956e75e52a0a61dec683e6..0c27ea6d914ea9abbada0637841cd754dbc29742 100644 (file)
@@ -764,7 +764,10 @@ class Connection(Connectable):
         else:
             if self._is_future:
                 raise exc.InvalidRequestError(
-                    "a transaction is already begun for this connection"
+                    "This connection has already initialized a SQLAlchemy "
+                    "Transaction() object via begin() or autobegin; can't "
+                    "call begin() here unless rollback() or commit() "
+                    "is called first."
                 )
             else:
                 return MarkerTransaction(self)
index 75bca190502ba3053b71046cd8149e87a2990531..1adb88617450cf85509b703a03538d8318f7d1fb 100644 (file)
@@ -631,8 +631,10 @@ class DefaultDialect(interfaces.Dialect):
             if trans_objs:
                 if connection._is_future:
                     raise exc.InvalidRequestError(
-                        "This connection has already begun a transaction; "
-                        "%s may not be altered until transaction end"
+                        "This connection has already initialized a SQLAlchemy "
+                        "Transaction() object via begin() or autobegin; "
+                        "%s may not be altered unless rollback() or commit() "
+                        "is called first."
                         % (", ".join(name for name, obj in trans_objs))
                     )
                 else:
index b8e7edc6522e1ea74895d268053b7cc21eff31e3..9e614202237758580b0b272950508ccbbbc6dc6b 100644 (file)
@@ -1568,8 +1568,10 @@ class FutureTransactionTest(fixtures.FutureEngineMixin, fixtures.TablesTest):
         with testing.db.begin() as conn:
             assert_raises_message(
                 exc.InvalidRequestError,
-                "This connection has already begun a transaction; "
-                "isolation_level may not be altered until transaction end",
+                r"This connection has already initialized a SQLAlchemy "
+                r"Transaction\(\) object via begin\(\) or autobegin; "
+                r"isolation_level may not be altered unless rollback\(\) or "
+                r"commit\(\) is called first.",
                 conn.execution_options,
                 isolation_level="AUTOCOMMIT",
             )
@@ -1582,8 +1584,10 @@ class FutureTransactionTest(fixtures.FutureEngineMixin, fixtures.TablesTest):
 
             assert_raises_message(
                 exc.InvalidRequestError,
-                "This connection has already begun a transaction; "
-                "isolation_level may not be altered until transaction end",
+                r"This connection has already initialized a SQLAlchemy "
+                r"Transaction\(\) object via begin\(\) or autobegin; "
+                r"isolation_level may not be altered unless rollback\(\) or "
+                r"commit\(\) is called first.",
                 conn.execution_options,
                 isolation_level="AUTOCOMMIT",
             )
@@ -1822,7 +1826,10 @@ class FutureTransactionTest(fixtures.FutureEngineMixin, fixtures.TablesTest):
 
             assert_raises_message(
                 exc.InvalidRequestError,
-                "a transaction is already begun for this connection",
+                r"This connection has already initialized a SQLAlchemy "
+                r"Transaction\(\) object via begin\(\) or autobegin; can't "
+                r"call begin\(\) here unless rollback\(\) or commit\(\) is "
+                r"called first.",
                 conn.begin,
             )