starlette icon indicating copy to clipboard operation
starlette copied to clipboard

Transactions error

Open jiangshujsf opened this issue 5 years ago • 0 comments

1.https://www.starlette.io/database/ Using the low-level API:

async def populate_note(request):
    transaction = await database.transaction()
    try:
        # This database insert occurs within a transaction.
        # It will be rolled back by the `RuntimeError`.
        query = notes.insert().values(text="you won't see me", completed=True)
        await database.execute(query)
        raise RuntimeError()
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

missing 'await' before ' transaction.rollback()' and ' transaction.commit()'

2.but in https://www.encode.io/databases/connections_and_transactions/

transaction = await database.transaction()
try:
    await transaction.start()
    ...
except:
    await transaction.rollback()
else:
    await transaction.commit()

it's unable work nomaly,and after reomve 'await transaction.start()',it's working. why?

jiangshujsf avatar Feb 17 '21 13:02 jiangshujsf