update:优化了websocket对数据库连接的获取与释放
This commit is contained in:
1 parent
ce46c0f35b
commit
5a47440e0f
8 files changed
+91
-54
No files matched your search
+11
-2
@@ -1,8 +1,10 @@
|
||||
from sqlalchemy import create_engine, Column, Integer, String, CHAR
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker, Session
|
||||
from contextlib import contextmanager
|
||||
from config import Config
|
||||
|
||||
|
||||
engine = create_engine(Config.SQLITE_URL, connect_args={"check_same_thread": False})
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base = declarative_base()
|
||||
@@ -30,10 +32,17 @@ class Assistant(Base):
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
def get_db():
|
||||
@contextmanager
|
||||
def get_db_context():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in new issue
Block a user