二次修改
This commit is contained in:
@@ -89,7 +89,7 @@ def get_db():
|
||||
return _db
|
||||
|
||||
|
||||
def initialize_sql_database(database_config):
|
||||
async def initialize_sql_database(database_config):
|
||||
"""
|
||||
根据配置初始化SQL数据库连接(SQLAlchemy版本)
|
||||
|
||||
@@ -119,7 +119,7 @@ def initialize_sql_database(database_config):
|
||||
# 使用SQLAlchemy初始化
|
||||
success = initialize_database_compat()
|
||||
if success:
|
||||
_sql_engine = get_engine()
|
||||
_sql_engine = await get_engine()
|
||||
logger.info("SQLAlchemy数据库初始化成功")
|
||||
else:
|
||||
logger.error("SQLAlchemy数据库初始化失败")
|
||||
|
||||
@@ -7,7 +7,6 @@ from sqlalchemy import Column, String, Float, Integer, Boolean, Text, Index, Dat
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from sqlalchemy.pool import QueuePool
|
||||
import os
|
||||
import datetime
|
||||
import time
|
||||
@@ -621,10 +620,9 @@ async def initialize_database():
|
||||
}
|
||||
|
||||
if config.database_type == "mysql":
|
||||
# MySQL连接池配置
|
||||
# MySQL连接池配置 - 异步引擎使用默认连接池
|
||||
engine_kwargs.update(
|
||||
{
|
||||
"poolclass": QueuePool,
|
||||
"pool_size": config.connection_pool_size,
|
||||
"max_overflow": config.connection_pool_size * 2,
|
||||
"pool_timeout": config.connection_timeout,
|
||||
@@ -640,10 +638,9 @@ async def initialize_database():
|
||||
}
|
||||
)
|
||||
else:
|
||||
# SQLite配置 - 添加连接池设置以避免连接耗尽
|
||||
# SQLite配置 - 异步引擎使用默认连接池
|
||||
engine_kwargs.update(
|
||||
{
|
||||
"poolclass": QueuePool,
|
||||
"pool_size": 20, # 增加池大小
|
||||
"max_overflow": 30, # 增加溢出连接数
|
||||
"pool_timeout": 60, # 增加超时时间
|
||||
@@ -678,6 +675,7 @@ async def get_db_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
raise RuntimeError("Database session not initialized")
|
||||
session = SessionLocal()
|
||||
yield session
|
||||
# await session.commit()
|
||||
except Exception:
|
||||
if session:
|
||||
await session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user