db connection pool bug 처리

upload
jaehwang 2026-05-18 13:48:22 +09:00
parent 602c69543c
commit c1f39aceff
1 changed files with 8 additions and 1 deletions

View File

@ -15,23 +15,30 @@ async def get_pool() -> aiomysql.Pool:
user=get_env("MYSQL_USER"),
password=get_env("MYSQL_PASSWORD"),
db=get_env("MYSQL_DB"),
autocommit=True,
charset="utf8mb4",
minsize=0,
maxsize=30,
connect_timeout=10,
)
return _pool
# 쓰기 (INSERT/UPDATE/DELETE)
async def execute(sql: str, args: tuple = ()) -> int:
pool = await get_pool()
async with pool.acquire() as conn:
await conn.ping(reconnect=True)
async with conn.cursor() as cur:
await cur.execute(sql, args)
await conn.commit()
return cur.lastrowid
# 읽기 (SELECT)
async def fetchone(sql: str, args: tuple = ()) -> dict | None:
pool = await get_pool()
async with pool.acquire() as conn:
await conn.ping(reconnect=True)
async with conn.cursor(aiomysql.DictCursor) as cur:
await cur.execute(sql, args)
return await cur.fetchone()