From eb01e8d2755a73f7c8db121d7b69b36bee1cae22 Mon Sep 17 00:00:00 2001 From: Jonathan Rohde Date: Mon, 24 Jun 2024 10:54:18 +0200 Subject: [PATCH] feat(sqlalchemy): use scoped session --- backend/apps/webui/internal/db.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/apps/webui/internal/db.py b/backend/apps/webui/internal/db.py index 6fd541f4e..b9bfc8aff 100644 --- a/backend/apps/webui/internal/db.py +++ b/backend/apps/webui/internal/db.py @@ -7,7 +7,7 @@ from typing_extensions import Self from sqlalchemy import create_engine, types, Dialect from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker +from sqlalchemy.orm import sessionmaker, scoped_session from sqlalchemy.sql.type_api import _T from config import SRC_LOG_LEVELS, DATA_DIR, DATABASE_URL, BACKEND_DIR @@ -61,10 +61,10 @@ Base = declarative_base() @contextmanager def get_session(): - db = SessionLocal() + session = scoped_session(SessionLocal) try: - yield db - db.commit() + yield session + session.commit() except Exception as e: - db.rollback() + session.rollback() raise e