Expand description
Transactions: the SqlTransaction handle, the with_transaction /
transaction brackets and the scoped transaction_layer.
Every way of running work in a transaction finishes it exactly once: it commits when the work succeeds and rolls back when the work fails, dies or is interrupted.
with_transaction/transactionare brackets around a closure. The commit is part of the bracket’s result, so a failedCOMMITis a typed error (E::from(SqlError)).transaction_layeris a scopedLayer(Effect.tsLayer.scoped(acquireRelease(..))) providingSqlTransactionService:BEGINwhen it is built, and, when its scope closes,COMMITif the scope’s owner succeeded orROLLBACKotherwise (the release reads the closingExitKind). A release cannot fail with a typed error, so a failedCOMMITthere is a defect (Cause::Die) of the owner.
Inside a transaction, savepoint is a nested bracket (SAVEPOINT, then
RELEASE SAVEPOINT on success or ROLLBACK TO SAVEPOINT otherwise), so a failure, defect or
interrupt of the inner work undoes only the inner work. with_transaction_options /
transaction_with begin with TransactionOptions (isolation level, read-only,
deferrable), and retry_serialization_failures reruns a transaction that lost a
serialization conflict (SQLSTATE 40001 / 40P01) as an id_effect Schedule allows.
Structs§
- SqlTransaction
Service - Capability: the open transaction of a
transaction_layer(derefs todyn SqlTransaction). - Transaction
Options - How
with_transaction_options/transaction_with/SqlClient::begin_withbegin a transaction. The default is the database’s default (noSET TRANSACTION).
Enums§
- Isolation
- SQL-standard transaction isolation level (
TransactionOptions::with_isolation).
Constants§
- SQLSTATE_
DEADLOCK_ DETECTED - SQLSTATE of a detected deadlock.
- SQLSTATE_
SERIALIZATION_ FAILURE - SQLSTATE of a serialization failure (
SERIALIZABLE/REPEATABLE READconflict).
Traits§
- SqlTransaction
- Logical SQL transaction: statements on it, then commit or rollback, as effects.
Functions§
- is_
serialization_ failure - Whether
erris a serialization failure (40001) or a deadlock (40P01): the transaction lost a conflict and running it again may succeed. - retry_
serialization_ failures - Runs
make()(typically a wholewith_transaction_options/transaction_with) and runs it again after a serialization failure asscheduleallows (e.g.Schedule::exponential(10ms)combined withSchedule::recurs(5)). Any other error, defect or interrupt is not retried; once the schedule is done the last error is returned. - savepoint
- A nested scope in the transaction
tx:SAVEPOINT(uninterruptibly),f, thenRELEASE SAVEPOINTwhenfsucceeds orROLLBACK TO SAVEPOINTwhen it fails, dies or is interrupted, so onlyf’s statements are undone and the outer transaction stays usable (catchf’s error to carry on).labelonly names the savepoint for logs: the name is generated (sp_<label>_<n>), never spliced from untrusted text. A failedSAVEPOINTorRELEASEfails withE::from(err). Savepoints nest. - transaction
with_transactionon theSqlClientServiceinR;f’s effect may need the rest ofR.- transaction_
layer - The scoped transaction
Layer: buildsSqlTransactionServicefrom theSqlClientServiceit needs (BEGIN, uninterruptibly), and when its scope closes commits (the owner succeeded) or rolls back (it failed, died or was interrupted). See the module docs for how a failedCOMMITsurfaces. - transaction_
with transactionbeginning withoptions, e.g.transaction_with(Isolation::Serializable, |tx| …).- with_
transaction - Begin a transaction on
client, runfwith it, then commit on success or roll back whenffails, dies or is interrupted. A failedBEGINorCOMMITfails withE::from(err). - with_
transaction_ options with_transactionbeginning withoptions(SqlClient::begin_with), e.g.with_transaction_options(client, Isolation::Serializable, f).
Type Aliases§
- SqlTransaction
Env - The minimal typed environment holding only
SqlTransactionService.