nsidnev/fastapi-realworld-example-app is using asyncpg.
There are two ways to use Transactions.
1. async with
statement
async with conn.transaction():
await repo_one.update_one(...)
await repo_two.insert_two(...)
await repo_three.delete_three(...)
# This automatically rolls back the transaction:
raise Exception
2. start
, rollback
, commit
statements
tx = conn.transaction()
await tx.start()
try:
await repo_one.update_one(...)
await repo_two.insert_two(...)
await repo_three.delete_three(...)
except:
await tx.rollback()
raise
else:
await tx.commit()
Getting the connection conn
in routes
Inject conn: Connection = Depends(_get_connection_from_pool)
.
from asyncpg.connection import Connection
from fastapi import Depends
from app.api.dependencies.database import _get_connection_from_pool
@router.post(
...
)
async def create_new_article(
...
conn: Connection = Depends(_get_connection_from_pool), # Add this
) -> ArticleInResponse: