fix db session
This commit is contained in:
@ -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:
|
||||
"""
|
||||
数据库操作基类
|
||||
|
Reference in New Issue
Block a user