수정본
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
"""
|
||||
데이터베이스 연결 모듈
|
||||
|
||||
- SQLAlchemy async 엔진 및 세션 팩토리 (PostgreSQL + asyncpg)
|
||||
- Base 모델 선언, get_db 의존성 제공
|
||||
"""
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
@@ -10,14 +16,19 @@ if DATABASE_URL.startswith("postgresql://"):
|
||||
|
||||
engine = create_async_engine(
|
||||
DATABASE_URL,
|
||||
echo=settings.DEBUG,
|
||||
echo=False, # SQL 로그 비활성화 (성능 개선)
|
||||
future=True,
|
||||
pool_pre_ping=True,
|
||||
pool_size=10,
|
||||
max_overflow=20,
|
||||
pool_size=20, # 증가
|
||||
max_overflow=30, # 증가
|
||||
pool_recycle=3600, # 1시간마다 연결 재생성
|
||||
pool_timeout=30, # 타임아웃 설정
|
||||
connect_args={
|
||||
"timeout": 60,
|
||||
"command_timeout": 60,
|
||||
"server_settings": {
|
||||
"jit": "off" # JIT 비활성화로 성능 개선
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -25,14 +36,20 @@ AsyncSessionLocal = sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
async def get_db():
|
||||
"""DB 세션 생성 및 관리 (안전한 종료 보장)"""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
Reference in New Issue
Block a user