Preferred way to commit/rollback
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
When read about FastAPI’s dependency system with yield
state, its pretty tempting to use database transactions with them like so:
async def get_db(request: Request):
""" database: Database = Depends(get_db) """
database: Database = request.app.state.database
async with database.transaction():
yield database
But this is incorrect way to do it because if transaction would not commit (teardown code in async with
raises error) then there is no way to change response.
So this case about transactions would be good to note in docs for dependencies with yield
statement (that this is incorrect way). Maybe add link to starlette docs about transactions: https://www.starlette.io/database/#transactions.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
SQL Commit And Rollback - DigitalOcean
COMMIT and ROLLBACK are two such keywords which are used in order store and revert the process of data storage. These keywords are...
Read more >How to rollback using explicit SQL Server transactions
In this article, we will learn how to rollback using explicit SQL Server transactions.
Read more >Difference between COMMIT and ROLLBACK in SQL
COMMIT in SQL is a transaction control language that is used to permanently save the changes done in the transaction in tables/databases.
Read more >Should I commit or rollback a read transaction? - Stack Overflow
The transaction will be committed or rolled back one way or another, so best practice would be to always issue a commit if...
Read more >ROLLBACK TRANSACTION (Transact-SQL) - SQL Server
A transaction cannot be rolled back after a COMMIT TRANSACTION statement is executed, except when the COMMIT TRANSACTION is associated with a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could use middleware to have every endpoint automatically wrapped in transaction like us:
Good. I vote for 2 things: