diff --git a/app/command.py b/app/command.py index 7f6652f5..d8eb3f52 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 SessionLocal +from app.db import GlobalDB 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 = SessionLocal() + self._db = GlobalDB # 事件管理器 self.eventmanager = EventManager() # 插件管理器 diff --git a/app/db/__init__.py b/app/db/__init__.py index e1367cde..402022a5 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -14,6 +14,9 @@ Engine = create_engine(f"sqlite:///{settings.CONFIG_PATH}/user.db", # 数据库会话 SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=Engine) +# 全局使用的数据库会话 +GlobalDB = SessionLocal() + def get_db(): """ @@ -37,7 +40,7 @@ class DbOper: if db: self._db = db else: - self._db = SessionLocal() + self._db = GlobalDB def __del__(self): if self._db: diff --git a/app/db/init.py b/app/db/init.py index bea477b4..ca5d81f7 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, SessionLocal +from app.db import Engine, GlobalDB from app.db.models import Base from app.db.models.user import User from app.log import logger @@ -22,15 +22,14 @@ def init_db(): # 全量建表 Base.metadata.create_all(bind=Engine) # 初始化超级管理员 - _db = SessionLocal() - user = User.get_by_name(db=_db, name=settings.SUPERUSER) + user = User.get_by_name(db=GlobalDB, name=settings.SUPERUSER) if not user: user = User( name=settings.SUPERUSER, hashed_password=get_password_hash(settings.SUPERUSER_PASSWORD), is_superuser=True, ) - user.create(_db) + user.create(GlobalDB) def update_db(): diff --git a/app/plugins/__init__.py b/app/plugins/__init__.py index 60457c71..7c884fdd 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 SessionLocal +from app.db import GlobalDB 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 = SessionLocal() + self.db = GlobalDB # 插件数据 self.plugindata = PluginDataOper(self.db) # 处理链 diff --git a/app/scheduler.py b/app/scheduler.py index 3a2fbd3c..06d1f078 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 SessionLocal +from app.db import GlobalDB 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 = SessionLocal() + self._db = GlobalDB # 调试模式不启动定时服务 if settings.DEV: return