init
This commit is contained in:
@@ -3,7 +3,6 @@ import sys
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# 添加项目根目录到Python路径
|
||||
project_root = Path(__file__).parent.parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
@@ -12,6 +11,7 @@ from sqlalchemy import text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from config.settings import settings
|
||||
import asyncio
|
||||
from contextlib import asynccontextmanager
|
||||
from utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@@ -67,13 +67,17 @@ class DatabaseManager:
|
||||
self.is_connected = False
|
||||
logger.info("数据库连接已断开")
|
||||
|
||||
@asynccontextmanager
|
||||
async def session(self):
|
||||
"""获取数据库会话的异步上下文管理器"""
|
||||
if not self.is_connected:
|
||||
await self.connect()
|
||||
|
||||
async with self.async_session() as session:
|
||||
session = self.async_session()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
async def get_session(self) -> AsyncSession:
|
||||
"""获取数据库会话"""
|
||||
|
||||
Reference in New Issue
Block a user