fix db session

This commit is contained in:
jxxghp
2023-10-18 18:30:09 +08:00
parent 68e16d18fe
commit 2e4536edb6
10 changed files with 48 additions and 48 deletions

View File

@ -1,5 +1,3 @@
import threading
from sqlalchemy import create_engine, QueuePool
from sqlalchemy.orm import sessionmaker, Session, scoped_session
@ -16,14 +14,11 @@ Engine = create_engine(f"sqlite:///{settings.CONFIG_PATH}/user.db",
max_overflow=10,
connect_args={"timeout": 60})
# 会话工厂
SessionFactory = sessionmaker(autocommit=False, autoflush=False, bind=Engine)
SessionFactory = sessionmaker(bind=Engine)
# 多线程全局使用的数据库会话
ScopedSession = scoped_session(SessionFactory)
# 数据库锁
DBLock = threading.Lock()
def get_db():
"""
@ -39,18 +34,6 @@ def get_db():
db.close()
def db_lock(func):
"""
使用DBLock加锁防止多线程同时操作数据库
装饰器
"""
def wrapper(*args, **kwargs):
with DBLock:
return func(*args, **kwargs)
return wrapper
class DbOper:
"""
数据库操作基类