Skip to content

Commit

Permalink
Call later to fix SQLAlchemy error
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
sbrunner committed Oct 3, 2023
1 parent a2a28a1 commit c274c5b
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions c2cwsgiutils/request_tracking/_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
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})


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)

0 comments on commit c274c5b

Please sign in to comment.