Skip to content

Commit d5fb100

Browse files
committed
添加异步session装饰器
1 parent d014576 commit d5fb100

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from functools import wraps
2+
from app.database.database import AsyncSessionLocal
3+
4+
5+
def with_async_session(func):
6+
@wraps(func)
7+
async def wrapper(*args, **kwargs):
8+
async with AsyncSessionLocal() as session:
9+
try:
10+
return await func(*args, session=session, **kwargs)
11+
except Exception:
12+
await session.rollback()
13+
raise
14+
finally:
15+
await session.close()
16+
17+
return wrapper

0 commit comments

Comments
 (0)