diff --git a/app/db/__init__.py b/app/db/__init__.py index a959b0ae..dda252a2 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -1,8 +1,13 @@ +import threading + from sqlalchemy import create_engine, QueuePool from sqlalchemy.orm import sessionmaker, Session, scoped_session from app.core.config import settings +# 数据库锁 +DBLock = threading.Lock() + # 数据库引擎 Engine = create_engine(f"sqlite:///{settings.CONFIG_PATH}/user.db", pool_pre_ping=True, @@ -44,4 +49,5 @@ class DbOper: if db: self._db = db else: - self._db = ScopedSession() + with DBLock: + self._db = ScopedSession() diff --git a/app/db/models/__init__.py b/app/db/models/__init__.py index 0bd8154c..94daa022 100644 --- a/app/db/models/__init__.py +++ b/app/db/models/__init__.py @@ -3,10 +3,7 @@ from typing import Any, Self, List from sqlalchemy.orm import as_declarative, declared_attr, Session -from app.db import ScopedSession - -# 数据库锁 -DBLock = threading.Lock() +from app.db import ScopedSession, DBLock def db_persist(func):