]> 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:36:43 +0000 (13:36 -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

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

index c60a8383fa0b72783d2dbd1c4ab682d0e4dfa446..5c1a159a690943682dec5d26b88a7f04bae4d6f3 100644 (file)
@@ -768,7 +768,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 9ec6783b02e104df7e209076b4136d8427cd45e1..b6dae6abc6c4f71e28625016e8d2fb35c26d7444 100644 (file)
@@ -612,8 +612,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,
             )