Principle:APIJSON APIJSON Transaction Management
| Knowledge Sources | |
|---|---|
| Domains | ORM, Transaction_Management, Database |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A database transaction pattern that wraps all write operations in a single API request within one atomic transaction, ensuring that multi-table modifications either all succeed or all fail together.
Description
Transaction Management in APIJSON ensures atomicity for multi-operation requests. When a request contains multiple table operations (e.g., create a Comment and update a Moment's comment count), all operations are executed within a single database transaction:
- Begin: Before parsing, a transaction is started on the database connection
- Execute: All table operations in the request tree are executed sequentially within the transaction
- Commit: If all operations succeed, the transaction is committed
- Rollback: If any operation fails, the entire transaction is rolled back
Query methods (GET/HEAD/GETS/HEADS) skip transaction management entirely. Write methods (POST/PUT/DELETE) always use transactions. The CRUD method, which can contain mixed operations, wraps all operations in a single transaction.
The problem solved is maintaining data consistency across related operations. Without transactions, a partial failure (e.g., Comment created but Moment count not updated) would leave the database in an inconsistent state.
Usage
Use this principle when:
- A single API request modifies multiple tables
- Related operations must be atomic
- Partial failure should not leave the database in an inconsistent state
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def execute_with_transaction(request, method):
if is_query_method(method):
return execute_without_transaction(request)
begin_transaction(isolation_level)
try:
result = recursively_parse(request)
commit_transaction()
return result
except Exception as e:
rollback_transaction()
return build_error_response(e)
# ACID properties ensured:
# Atomicity: All operations succeed or all fail
# Consistency: Database remains in valid state
# Isolation: Transaction is isolated from concurrent operations
# Durability: Committed changes persist