-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
override join_transaction_mode if engine_connect leaves the transaction open #11163
Comments
the Session's handling of a connection is determined by the join_transaction_mode setting, which defaults to So to have the @event.listens_for(engine, "engine_connect")
def on_engine_connect(connection, branch):
"""If this event handler executes any SQL, the later session.commit()
emits ROLLBACK instead of COMMIT.
"""
connection.execute(text("SELECT 42"))
connection.rollback() or set join transaction to control_fully:
im not sure where would be the best place to add a note for this. |
I did try both of the snippets you show when sussing out it was the SQLAlchemy 2 transaction changes. It's worth noting that |
we should adjust that doc to include "including when the connection is given to it by an Engine that has had its behavior modified by an event hook" |
overall I think we need a note on "engine_connect" that starting a transaction inside the event changes the expected behavior of the engine if that transaction is left open |
Federico Caselli has proposed a fix for this issue in the main branch: Improve docs wrt transactions and event with session https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5228 |
Federico Caselli has proposed a fix for this issue in the rel_2_0 branch: Improve docs wrt transactions and event with session https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/5231 |
Describe the bug
I'm migrating our codebase to SQLAlchemy 2.0. After enabling
future=True
, I observed that sessions were being rolled back whensession.commit()
is called.I've minimised the problem to an example that reproduces on SQLAlchemy 1.4 and 2.0.
Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected
No response
SQLAlchemy Version in Use
1.4.52, 2.0.28
DBAPI (i.e. the database driver)
appears to be any but psycopg2 and whatever sqlite:// uses by default
Database Vendor and Major Version
PostgreSQL 12, 14
Python Version
3.10
Operating system
Linux, macOS
To Reproduce
Error
The log output below shows a
ROLLBACK
instead of the expectedCOMMIT
.Additional context
I think I understand that the transaction changes in SQLAlchemy 2 is the cause. I was able to change our code to use a different event instead so I don't need a solution, but I feel like this interaction is extremely non-obvious and maybe the events need documentation about managing transactions which may affect the session.
The text was updated successfully, but these errors were encountered: