Skip to content

Commit

Permalink
add NullPool as pool strategy (#32)
Browse files Browse the repository at this point in the history
* add NullPool

* minor fix
  • Loading branch information
lionick authored Oct 26, 2023
1 parent c3fd112 commit 5108499
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/database.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 5108499

Please sign in to comment.