From c274c5bfec992adb13dd0c63988fe3e583f6bd00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Tue, 3 Oct 2023 15:55:11 +0200 Subject: [PATCH] Call later to fix SQLAlchemy error sqlalchemy.exc.InvalidRequestError: This session is provisioning a new connection; concurrent operations are not permitted (Background on this error at: https://sqlalche.me/e/20/isce) --- c2cwsgiutils/request_tracking/_sql.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/c2cwsgiutils/request_tracking/_sql.py b/c2cwsgiutils/request_tracking/_sql.py index fd151b7ef..f75fb28cb 100644 --- a/c2cwsgiutils/request_tracking/_sql.py +++ b/c2cwsgiutils/request_tracking/_sql.py @@ -5,7 +5,7 @@ from sqlalchemy.orm import Session -def _add_session_id(session: Session, _transaction: Any, _connection: Any) -> None: +def _add_session_id(session: Session, _transaction: Any) -> None: request = get_current_request() if request is not None: session.execute("set application_name=:session_id", params={"session_id": request.c2c_request_id}) @@ -13,4 +13,5 @@ def _add_session_id(session: Session, _transaction: Any, _connection: Any) -> No def init() -> None: """Initialize the SQL alchemy session selector.""" - sqlalchemy.event.listen(Session, "after_begin", _add_session_id) + + sqlalchemy.event.listen(Session, "after_transaction_create", _add_session_id)