From f2eded951ee2b702c5e5f92b4e8c535fdc014ad5 Mon Sep 17 00:00:00 2001 From: Davide Mendolia Date: Sun, 22 Feb 2026 21:54:23 +0100 Subject: [PATCH] fix(python): commit read-only sessions to avoid spurious rollbacks Every GET request opened an implicit PostgreSQL transaction that was rolled back when the session closed, generating ~200/s rollbacks visible in transaction count metrics during benchmarks. Wrapping the session yield in try/commit/except/rollback ensures: - Read-only transactions are committed (no-op for data, counts as commit in pg metrics instead of rollback) - Write operations that already committed inside the repository have no active transaction left, so the outer commit is a no-op - Exceptions still trigger an explicit rollback and re-raise Co-Authored-By: Claude Sonnet 4.6 --- .../src/openapi_server/infrastructure/database/database.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/python/src/openapi_server/infrastructure/database/database.py b/src/python/src/openapi_server/infrastructure/database/database.py index 295824c9..7de54f48 100644 --- a/src/python/src/openapi_server/infrastructure/database/database.py +++ b/src/python/src/openapi_server/infrastructure/database/database.py @@ -76,4 +76,9 @@ async def my_endpoint(session: AsyncSession = Depends(db_manager.get_session)): pass """ async with self.async_session_factory() as session: - yield session + try: + yield session + await session.commit() + except Exception: + await session.rollback() + raise