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
| Type | Description |
|---|---|
DEFERRED | Default. No locks are acquired until the first read or write operation. A read operation acquires a shared lock; a write operation acquires a write lock. |
IMMEDIATE | Acquires a write lock immediately when BEGIN is executed. Other connections can still read but cannot write until the transaction completes. |
EXCLUSIVE | Same behavior as IMMEDIATE in Turso. Turso always uses WAL mode, where EXCLUSIVE and IMMEDIATE are equivalent. |
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:Turso does not support SAVEPOINT or nested transactions. Each connection can have at most one active transaction at a time.
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