liveBook · Manning

Database transaction

As explained in Build Python Web Apps with Streamlit

A database transaction is an atomic unit of database work whose modifications remain provisional until committed or are discarded by rollback.

A database transaction is a temporary workspace for database changes. Write operations such as INSERT, UPDATE, and DELETE remain invisible to other users until the application commits them. Committing confirms the changes and makes them permanent; rolling back discards temporary changes. section 8.2.5

A practical database layer commits only successful write queries. It catches execution errors, rolls back the affected transaction, and re-raises the exception so the application can handle or display the failure instead of retaining partial changes. section 8.2.5

Commit successful writes and undo failures

A SQLAlchemy session executes a parameterized query inside a try block. When write is true, session.commit() saves the changes. If execution raises an exception, session.rollback() restores the database before the exception is re-raised.

How Build Python Web Apps with Streamlit covers it

How deep it goes
Explains transactions through SQLAlchemy sessions, commit, rollback, and exception handling without detailing isolation levels or concurrency.
Examples
Demonstrates a Streamlit haiku application that commits successful write queries and rolls them back after errors.
What it assumes
Assumes basic SQL write operations and Python exception handling while defining commit and rollback explicitly.
What you can do after
Operate a SQLAlchemy-backed Streamlit database layer that commits successful writes and rolls back failed write operations.
How it connects
Connects transactions with SQLAlchemy sessions, SQL write operations, exception handling, commit, and rollback.

Also explained in

Compare how 14 books explain database transaction →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.