Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

404
Vistas
How to apply transaction logic in FastAPI RealWorld example app?

I am using nsidnev/fastapi-realworld-example-app.

I need to apply transaction logic to this project.

In one API, I am calling a lot of methods from repositories and doing updating, inserting and deleting operations in many tables. If there is an exception in any of these operations, how can I roll back changes? (Or if everything is correct then commit.)

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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:
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda