From 374e633ca79405dcce7039a0a1f7257e4e347ed0 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 2 Sep 2023 08:18:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E4=BC=9A=E8=AF=9D=20#330?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command.py | 4 ++-- app/db/__init__.py | 14 +++++++------- app/db/init.py | 8 +++++--- app/plugins/__init__.py | 4 ++-- app/scheduler.py | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/command.py b/app/command.py index d8eb3f52..3de0d7e8 100644 --- a/app/command.py +++ b/app/command.py @@ -13,7 +13,7 @@ from app.chain.transfer import TransferChain from app.core.event import Event as ManagerEvent from app.core.event import eventmanager, EventManager from app.core.plugin import PluginManager -from app.db import GlobalDB +from app.db import ScopedSession from app.log import logger from app.schemas.types import EventType, MessageChannel from app.utils.object import ObjectUtils @@ -41,7 +41,7 @@ class Command(metaclass=Singleton): def __init__(self): # 数据库连接 - self._db = GlobalDB + self._db = ScopedSession() # 事件管理器 self.eventmanager = EventManager() # 插件管理器 diff --git a/app/db/__init__.py b/app/db/__init__.py index 402022a5..d90df159 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -1,5 +1,5 @@ from sqlalchemy import create_engine, QueuePool -from sqlalchemy.orm import sessionmaker, Session +from sqlalchemy.orm import sessionmaker, Session, scoped_session from app.core.config import settings @@ -11,11 +11,11 @@ Engine = create_engine(f"sqlite:///{settings.CONFIG_PATH}/user.db", pool_size=1000, pool_recycle=60 * 10, max_overflow=0) -# 数据库会话 -SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=Engine) +# 会话工厂 +SessionFactory = sessionmaker(autocommit=False, autoflush=False, bind=Engine) -# 全局使用的数据库会话 -GlobalDB = SessionLocal() +# 多线程全局使用的数据库会话 +ScopedSession = scoped_session(SessionFactory) def get_db(): @@ -25,7 +25,7 @@ def get_db(): """ db = None try: - db = SessionLocal() + db = SessionFactory() yield db finally: if db: @@ -40,7 +40,7 @@ class DbOper: if db: self._db = db else: - self._db = GlobalDB + self._db = ScopedSession() def __del__(self): if self._db: diff --git a/app/db/init.py b/app/db/init.py index ca5d81f7..6013bcc8 100644 --- a/app/db/init.py +++ b/app/db/init.py @@ -6,7 +6,7 @@ from alembic.config import Config from app.core.config import settings from app.core.security import get_password_hash -from app.db import Engine, GlobalDB +from app.db import Engine, ScopedSession from app.db.models import Base from app.db.models.user import User from app.log import logger @@ -22,14 +22,16 @@ def init_db(): # 全量建表 Base.metadata.create_all(bind=Engine) # 初始化超级管理员 - user = User.get_by_name(db=GlobalDB, name=settings.SUPERUSER) + db = ScopedSession() + user = User.get_by_name(db=db, name=settings.SUPERUSER) if not user: user = User( name=settings.SUPERUSER, hashed_password=get_password_hash(settings.SUPERUSER_PASSWORD), is_superuser=True, ) - user.create(GlobalDB) + user.create(db) + db.close() def update_db(): diff --git a/app/plugins/__init__.py b/app/plugins/__init__.py index 7c884fdd..d602aa27 100644 --- a/app/plugins/__init__.py +++ b/app/plugins/__init__.py @@ -5,7 +5,7 @@ from typing import Any, List, Dict, Tuple from app.chain import ChainBase from app.core.config import settings from app.core.event import EventManager -from app.db import GlobalDB +from app.db import ScopedSession from app.db.models import Base from app.db.plugindata_oper import PluginDataOper from app.db.systemconfig_oper import SystemConfigOper @@ -37,7 +37,7 @@ class _PluginBase(metaclass=ABCMeta): def __init__(self): # 数据库连接 - self.db = GlobalDB + self.db = ScopedSession() # 插件数据 self.plugindata = PluginDataOper(self.db) # 处理链 diff --git a/app/scheduler.py b/app/scheduler.py index c7e7ea2d..d8150247 100644 --- a/app/scheduler.py +++ b/app/scheduler.py @@ -12,7 +12,7 @@ from app.chain.rss import RssChain from app.chain.subscribe import SubscribeChain from app.chain.transfer import TransferChain from app.core.config import settings -from app.db import GlobalDB +from app.db import ScopedSession from app.log import logger from app.utils.singleton import Singleton from app.utils.timer import TimerUtils @@ -40,7 +40,7 @@ class Scheduler(metaclass=Singleton): def __init__(self): # 数据库连接 - self._db = GlobalDB + self._db = ScopedSession() # 调试模式不启动定时服务 if settings.DEV: return