Transaction PRO
This section describes the transaction model, transaction types.
The SQLite transaction API uses a step-based, declarative model to provide predictable, controlled, and safe transaction behavior at the scripting level.
Transaction Overview
A transaction groups multiple database operations into a single atomic unit:
- All steps succeed → the transaction is committed
- Any step fails → the transaction is rolled back
- After rollback, the database remains unchanged
SQLite guarantees consistency and isolation internally. JavaScript code does not need to manually handle rollback logic.
transaction
transaction executes a transaction defined by an ordered list of SQL steps.
Transaction Steps
Each transaction step consists of:
sql: the SQL statement to executeargs: optional bound parameters
Example:
Steps are executed sequentially in the order they are declared.
Transaction Kinds
Transactions support three kinds, corresponding to SQLite’s native transaction modes:
deferred
The default transaction kind.
- No locks are acquired when the transaction begins
- Locks are obtained only when the first read or write occurs
- Suitable for most general-purpose transaction scenarios
immediate
- Attempts to acquire a write lock immediately when the transaction begins
- Fails immediately if the write lock cannot be acquired
- Useful when subsequent write operations must be guaranteed to proceed
exclusive
- Acquires an exclusive lock when the transaction begins
- Blocks all other read and write operations
- Intended for scenarios that require full database exclusivity
Error Handling and Rollback
A transaction will be rolled back automatically if any of the following occur:
- SQL execution errors
- Parameter binding errors
- Constraint violations (unique, foreign key, etc.)
- Database lock conflicts
Example:
Usage Recommendations
- Group operations that must succeed together into a single transaction
- Prefer the default
deferredtransaction kind - Use
immediatewhen early write-lock acquisition is required - Avoid long-running or unrelated work inside transactions
- Do not rely on conditional logic to determine transaction steps
Next Steps
After working with transactions, you may want to:
- Create and manage table schemas
- Define indexes and constraints
- Inspect database schema information
Continue with:
- Schema Management
- Schema Introspection
