Midwess
Guides

Start a transaction from a PGlite handle:

let tx = db.transaction().await?;
tx.exec("INSERT INTO users (name) VALUES ('alice')").await?;
let rows = tx.query("SELECT count(*) FROM users", &[]).await?;
tx.commit().await?;

Rollback

Call rollback() explicitly:

let tx = db.transaction().await?;
tx.exec("INSERT INTO users (name) VALUES ('discarded')").await?;
tx.rollback().await?;

If the transaction value is dropped before commit() or rollback(), the implementation sends ROLLBACK.

API

Method Purpose
exec(sql) Execute SQL without returning rows.
query(sql, params) Execute SQL with bind parameters and return rows.
commit() Commit and consume the transaction.
rollback() Roll back and consume the transaction.

Single-process note

In single-process mode, the database uses a transaction lock so other db.exec() or db.query() calls do not interleave inside a transaction.