--- name: sqlx-sqlite-transactions description: "Use when Rust SQLx writes survive a SQLite rollback, an operation hangs on a small pool, or multiple statements must commit together." category: database risk: critical source: self source_type: self date_added: "2026-12-01" author: ShianMike tags: [rust, sqlx, sqlite, connection-pool, transactions, rollback] --- # SQLx SQLite Transactions ## When to Use - A Rust repository method leaves partial database state after an error or rollback. - A request holds a transaction and times out while acquiring another SQLx pool connection. - A service creates or updates several related rows and needs to prove they commit as one unit. ## Prerequisites - Inspect the installed SQLx version and database/runtime features in `Cargo.toml` and `Cargo.lock`. The example uses SQLx 0.9.1 with Tokio and SQLite. - Identify which method owns the transaction and the first statement that reads or writes through it. Record pool size and acquire timeout. - Use a fresh SQLite file for a multi-connection test and record `SELECT sqlite_version()`. A memory database can have different connection-sharing behavior depending on its options. ## 1. Trace the executor through each helper 1. Start with `pool.begin().await?` at the boundary that owns the unit of work. It checks out a connection for the lifetime of the transaction. 2. Follow each `execute`, `fetch_one`, or `fetch_optional` call across helper methods. A call sent to `&SqlitePool` checks out its own connection, even when the caller holds a transaction. An independent write can commit immediately. 3. If the pool has a one-connection limit and the transaction owns the connection, a stray pool call can wait for a connection that the same operation is holding. Check the executor and distinguish pool acquisition from an SQLite lock error before tuning limits. ## 2. Keep dependent statements on the transaction connection 1. Execute dependent reads and writes with `&mut *tx` at a call site that owns `tx`. For a helper taking `&mut Transaction<'_, Sqlite>`, use `&mut **tx` at SQLx query calls. Alternatively, accept `&mut SqliteConnection` and pass the transaction's underlying connection. 2. Commit only after all required statements succeed. Explicitly roll back a failed operation when you need to observe rollback errors. Report a rollback failure alongside the original operation error. 3. Keep independent or slow external work out of the write transaction. Use bound values for SQL inputs. ```rust let mut tx = pool.begin().await?; sqlx::query("UPDATE orders SET status = ?1 WHERE id = ?2") .bind("ready") .bind(order_id) .execute(&mut *tx) .await?; tx.commit().await?; ``` ## 3. Assert the state after failure and success 1. Run the real SQLx/SQLite code with at least two pool slots so the fixture can expose a write on the second connection. 2. Force a database error in a later statement, verify its structured error code, and assert persisted state after rollback. Repeat with valid data and assert both writes persist after commit. 3. If changing when a writer acquires its SQLite lock, test that separately with contention. A different begin mode can change where an error occurs while the helper still uses the wrong executor. ## Example Run the [two-connection rollback fixture](references/pool-write-survives-rollback.md). It checks row counts for a failed pooled insert sequence, a failed transactional sequence, and a successful transaction. ## Limitations - The example targets file-backed SQLite with a deferred transaction and two pool slots. SQLite lock timing depends on the journal mode and whether the transaction has already touched the database. - The fixture covers transaction routing, a constraint failure, and a successful commit. It leaves savepoints, cancellation, retries, and other database drivers untested. ## Safety and Side Effects - The fixture creates a disposable SQLite file and closes the pool before cleanup. Building can write a Cargo lockfile and build artifacts in the scratch project. - Use a disposable copy for failure-path tests. Test constraints and rollback behavior against representative data before changing production writes. ## Related Skills and Documentation - For connection pragmas and contention, see [SQLite Development](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/sqlite-development/SKILL.md). - For Rust HTTP handlers and state, see [Axum Development](https://github.com/sickn33/agentic-awesome-skills/blob/main/skills/axum-development/SKILL.md). - The SQLx API documents [Pool](https://docs.rs/sqlx/latest/sqlx/struct.Pool.html), [Transaction](https://docs.rs/sqlx/latest/sqlx/struct.Transaction.html), and the [Executor trait](https://docs.rs/sqlx/latest/sqlx/trait.Executor.html). - SQLite documents [transaction modes](https://www.sqlite.org/lang_transaction.html) and [extended result codes](https://www.sqlite.org/rescode.html#constraint_check).