Transactions
Transactions group multiple SQL statements into a single atomic unit of work. Either all statements in a transaction succeed and are committed, or the entire transaction is rolled back and no changes are applied. Turso supports explicit transactions with BEGIN, COMMIT, and ROLLBACK, and also runs each standalone statement in an implicit transaction (autocommit mode).Syntax
BEGIN
The BEGIN statement starts a new explicit transaction. While a transaction is open, all subsequent statements are part of that transaction until a COMMIT or ROLLBACK is issued.Transaction Types
If no type is specified, the transaction defaults to
DEFERRED.
COMMIT / END
The COMMIT statement (or its alias END) finalizes the transaction and writes all changes to the database. Once committed, changes are durable and visible to other connections.ROLLBACK
The ROLLBACK statement aborts the current transaction and reverts all changes made since BEGIN. The database is left in the state it was in before the transaction started.Implicit Transactions
When no explicit transaction is active, Turso wraps each individual statement in an implicit transaction. This is called autocommit mode. Each statement is automatically committed after it executes successfully, or automatically rolled back if it fails.Transaction Lifecycle
A typical transaction lifecycle:Savepoints
Savepoints are named markers within a transaction that let you roll back part of the work without aborting the whole transaction. They can be nested.While a write statement on the same connection is still in progress,
SAVEPOINT, RELEASE, and ROLLBACK TO may return SQLITE_BUSY.BEGIN CONCURRENT
Turso Extension: BEGIN CONCURRENT is a Turso-specific extension not available in standard SQLite. It requires MVCC mode.
Syntax
Requirements
BEGIN CONCURRENT requires MVCC mode. Enable it by setting the journal mode before opening any transactions:How It Works
A concurrent transaction operates on a snapshot of the database taken at the time of BEGIN CONCURRENT. Each connection reads from its own snapshot and writes independently. When COMMIT is issued, Turso checks whether any other transaction has modified the same rows since the snapshot was taken:- If there is no conflict, the transaction commits successfully.
- If a write-write conflict is detected, the COMMIT fails and the transaction must be rolled back and retried.
Example
See Also
- INSERT for inserting rows
- UPDATE for updating rows
- DELETE for deleting rows
- Compatibility for differences from SQLite