From 5108499ba5ebc4b4deca7cc444914f1236c3e305 Mon Sep 17 00:00:00 2001 From: lionick <44701697+lionick@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:07:30 +0300 Subject: [PATCH] add NullPool as pool strategy (#32) * add NullPool * minor fix --- app/database.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/database.py b/app/database.py index 5e97b80..208398a 100644 --- a/app/database.py +++ b/app/database.py @@ -1,10 +1,14 @@ from app.utils import configParser +from app.logger import log from sqlalchemy import create_engine from sqlmodel import Session from sqlalchemy.exc import OperationalError +from sqlalchemy.pool import NullPool class Database: + logger = log.get_logger("database") + def __init__(self): config_file = 'config.global.py' @@ -14,7 +18,7 @@ def __init__(self): pool_size = int(db_params.get('pool_size', 25)) max_overflow = int(db_params.get('max_overflow', 5)) - self.engine = create_engine(url, pool_size=pool_size, max_overflow=max_overflow) + self.engine = create_engine(url, poolclass=NullPool) def check_database_connection(self): try: @@ -27,8 +31,15 @@ def check_database_connection(self): return False def get_session(self): - with Session(self.engine) as session: + session = Session(self.engine) + try: yield session + except Exception: + self.logger.exception("Session rollback because of exception") + session.rollback() + raise + finally: + session.close() def create_session(self): return Session(self.engine)