From 01ad2713d2399d68681977d177fb405f79a8cdeb Mon Sep 17 00:00:00 2001 From: Eduardo Lauer Date: Fri, 10 Oct 2025 11:57:53 -0300 Subject: [PATCH 1/2] increase pool_size to avoid QueuePool limit --- src/db_config.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/db_config.py b/src/db_config.py index 29064ca..d2e324c 100644 --- a/src/db_config.py +++ b/src/db_config.py @@ -12,12 +12,16 @@ SQLALCHEMY_DATABASE_URL = os.environ["SQLALCHEMY_DATABASE_URL"] -engine = create_async_engine(SQLALCHEMY_DATABASE_URL) +engine = create_async_engine( + SQLALCHEMY_DATABASE_URL, + pool_size=15, + max_overflow=15, + pool_timeout=30, + ) sync_engine = create_engine(SQLALCHEMY_DATABASE_URL) async_session_maker = async_sessionmaker(engine, expire_on_commit=False) - # database models (SQLAlchemy) class Base(DeclarativeBase): # pylint: disable=too-few-public-methods """Classe base para modelos SQL Alchemy. From 11309df813d4c3118a573f23b20075d1b584ed32 Mon Sep 17 00:00:00 2001 From: Eduardo Lauer Date: Fri, 10 Oct 2025 11:59:39 -0300 Subject: [PATCH 2/2] Refactor DbContextManager --- src/db_config.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/db_config.py b/src/db_config.py index d2e324c..75ee739 100644 --- a/src/db_config.py +++ b/src/db_config.py @@ -87,10 +87,21 @@ class DbContextManager: """Context manager para manipulação de sessões do banco de dados. """ def __init__(self): - self.db = async_session_maker() + self.async_session_maker = async_session_maker + self.db = None async def __aenter__(self): + self.db = self.async_session_maker() return self.db async def __aexit__(self, exc_type, exc_value, traceback): - await self.db.close() + try: + if exc_type is None: + # 2. If NO exception occurred, commit the transaction. + await self.db.commit() + else: + # 3. If an exception DID occur, roll back the transaction. + await self.db.rollback() + finally: + # 4. ALWAYS close the session. + await self.db.close()