Skip to content

Add sqlalchemy connection pool config #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions backend/app/admin/tests/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from sqlalchemy.ext.asyncio import AsyncSession

from backend.database.db import create_database_url, create_engine_and_session
from backend.database.db import create_async_engine_and_session, create_database_url

TEST_SQLALCHEMY_DATABASE_URL = create_database_url(unittest=True)

_, async_test_db_session = create_engine_and_session(TEST_SQLALCHEMY_DATABASE_URL)
_, async_test_db_session = create_async_engine_and_session(TEST_SQLALCHEMY_DATABASE_URL)


async def override_get_db() -> AsyncSession:
async def override_get_db():
"""session 生成器"""
session = async_test_db_session()
try:
async with async_test_db_session() as session:
yield session
except Exception as se:
await session.rollback()
raise se
finally:
await session.close()
1 change: 1 addition & 0 deletions backend/core/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Settings(BaseSettings):

# Database
DATABASE_ECHO: bool = False
DATABASE_POOL_ECHO: bool = False
DATABASE_SCHEMA: str = 'fba'
DATABASE_CHARSET: str = 'utf8mb4'

Expand Down
22 changes: 17 additions & 5 deletions backend/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from fastapi import Depends
from sqlalchemy import URL
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine

from backend.common.log import log
from backend.common.model import MappedBase
Expand All @@ -32,11 +32,23 @@ def create_database_url(unittest: bool = False) -> URL:
return url


def create_engine_and_session(url: str | URL):
def create_async_engine_and_session(url: str | URL) -> tuple[AsyncEngine, async_sessionmaker[AsyncSession]]:
"""创建数据库引擎和 Session"""
try:
# 数据库引擎
engine = create_async_engine(url, echo=settings.DATABASE_ECHO, future=True, pool_pre_ping=True)
engine = create_async_engine(
url,
echo=settings.DATABASE_ECHO,
echo_pool=settings.DATABASE_POOL_ECHO,
future=True,
# 中等并发
pool_size=10, # 低:- 高:+
max_overflow=20, # 低:- 高:+
pool_timeout=30, # 低:+ 高:-
pool_recycle=3600, # 低:+ 高:-
pool_pre_ping=True, # 低:False 高:True
pool_use_lifo=False, # 低:False 高:True
)
# log.success('数据库连接成功')
except Exception as e:
log.error('❌ 数据库链接失败 {}', e)
Expand All @@ -52,7 +64,7 @@ async def get_db():
yield session


async def create_table():
async def create_table() -> None:
"""创建数据库表"""
async with async_engine.begin() as coon:
await coon.run_sync(MappedBase.metadata.create_all)
Expand All @@ -64,6 +76,6 @@ def uuid4_str() -> str:


SQLALCHEMY_DATABASE_URL = create_database_url()
async_engine, async_db_session = create_engine_and_session(SQLALCHEMY_DATABASE_URL)
async_engine, async_db_session = create_async_engine_and_session(SQLALCHEMY_DATABASE_URL)
# Session Annotated
CurrentSession = Annotated[AsyncSession, Depends(get_db)]