达梦数据库适配

上代码

1.db_connect.py

python 复制代码
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy import event, text

from project.core.config import settings
from project.core.config.settings import logger

if settings.DBConfig.CONNECT_METHOD == 'dm':
    # 达梦数据库引擎配置
    engine = create_async_engine(
        settings.DBConfig.DM.DM8_URL,
        echo=settings.DBConfig.DM.ECHO,  # 根据环境配置是否输出SQL
    )
else:
    # 创建mysql异步数据库引擎
    engine = create_async_engine(
        settings.DBConfig.MySql.MYSQL_URL,
        # 连接池配置
        pool_size=settings.DBConfig.MySql.POOL_SIZE,  # 4核 × 2 + 1
        max_overflow=settings.DBConfig.MySql.MAX_OVERFLOW,  # 允许临时增加5个连接
        pool_recycle=settings.DBConfig.MySql.POOL_RECYCLE,  # 30分钟回收连接
        pool_timeout=settings.DBConfig.MySql.POOL_TIMEOUT,  # 获取连接超时30秒
        pool_pre_ping=settings.DBConfig.MySql.POOL_PRE_PING,  # 自动检查连接有效性
        echo=settings.DBConfig.MySql.ECHO,  # 根据环境配置是否输出SQL
        connect_args={
            "connect_timeout": settings.DBConfig.MySql.CONNECT_TIMEOUT,
        }
    )

# 创建异步会话工厂
SessionLocal = sessionmaker(
    bind=engine,
    class_=AsyncSession,
    autocommit=False,
    autoflush=False,
    expire_on_commit=False  # 避免提交后属性访问问题
)

# 声明基类
Base = declarative_base()


async def verify_connection():
    """验证数据库连接是否可用"""
    async with engine.connect() as conn:
        await conn.execute(text("SELECT 1"))
        logger.info("Database connection verified successfully")


async def dispose_engine():
    """关闭连接池并释放所有连接"""
    await engine.dispose()
    logger.info("Database engine disposed successfully")

2.db_session.py

python 复制代码
from contextlib import asynccontextmanager
from fastapi import HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import SQLAlchemyError

from project.core.config.settings import logger
from project.core.database.db_connect import SessionLocal
import asyncio
from project.core.database.db_connect import verify_connection, dispose_engine


@asynccontextmanager
async def _get_db() -> AsyncSession:
    """内部使用的上下文管理器"""
    session = SessionLocal()
    try:
        yield session
        await session.commit()
    except SQLAlchemyError as e:
        await session.rollback()
        logger.error(f"Rollback failed: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Database operation failed: {str(e)}"
        ) from e
    finally:
        await session.close()


async def get_db() -> AsyncSession:
    """供路由使用的依赖项"""
    async with _get_db() as session:
        yield session


async def test_connection():
    try:
        await verify_connection()
    except Exception as e:
        logger.error(f"Database connection failed: {e}")
    finally:
        await dispose_engine()


asyncio.run(test_connection())

if __name__ == '__main__':
    # 测试数据库连接
    async def test_connection():
        try:
            await verify_connection()
            print("✅ Database connection test successful")
        except Exception as e:
            print(f"❌ Database connection failed: {e}")
        finally:
            await dispose_engine()


    asyncio.run(test_connection())
相关推荐
TiDB 社区干货传送门5 小时前
平凯数据库携手东软医保共筑智慧医保,构建可靠、安全、开放的数据底座
数据库·tidb·国产数据库
StarsGreen6 小时前
dumpsys gfxinfo数据信息分析方法
android
YYYing.7 小时前
【Mysql剑指offer系列 (七) 】MySQL性能调优和架构
数据库·mysql·八股
好的佩奇7 小时前
蓝牙之 GATT 数据模型
android·flutter·蓝牙
Decay__8 小时前
PostgreSQL WAL 归档与时间点恢复 —— WAL 连续归档、recovery_target 时点恢复与 pg_rewind
运维·数据库·sql·postgresql
Android小码家8 小时前
Systemui和Launcher是如何无限重启
android·framework
如夏倾城8 小时前
大模型(Large Model)解析:从数据类型、模型架构到任务选型
服务器·网络·数据库·语言模型·架构·audiolm
船神丿男人9 小时前
Redis 缓存击穿与雪崩实测:同键合并、TTL 错峰与迟到回填
数据库·redis·缓存
富贵儿99811 小时前
NL2SQL 实践手记 04:多表查询为什么更难——让模型找对表、连对关系
数据库·sql·ai·自然语言处理·langchain