Skip to main content

Module transaction

Module transaction 

Source
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 / transaction are brackets around a closure. The commit is part of the bracket’s result, so a failed COMMIT is a typed error (E::from(SqlError)).
  • transaction_layer is a scoped Layer (Effect.ts Layer.scoped(acquireRelease(..))) providing SqlTransactionService: BEGIN when it is built, and, when its scope closes, COMMIT if the scope’s owner succeeded or ROLLBACK otherwise (the release reads the closing ExitKind). A release cannot fail with a typed error, so a failed COMMIT there 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§

SqlTransactionService
Capability: the open transaction of a transaction_layer (derefs to dyn SqlTransaction).
TransactionOptions
How with_transaction_options / transaction_with / SqlClient::begin_with begin a transaction. The default is the database’s default (no SET 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 READ conflict).

Traits§

SqlTransaction
Logical SQL transaction: statements on it, then commit or rollback, as effects.

Functions§

is_serialization_failure
Whether err is 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 whole with_transaction_options / transaction_with) and runs it again after a serialization failure as schedule allows (e.g. Schedule::exponential(10ms) combined with Schedule::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, then RELEASE SAVEPOINT when f succeeds or ROLLBACK TO SAVEPOINT when it fails, dies or is interrupted, so only f’s statements are undone and the outer transaction stays usable (catch f’s error to carry on). label only names the savepoint for logs: the name is generated (sp_<label>_<n>), never spliced from untrusted text. A failed SAVEPOINT or RELEASE fails with E::from(err). Savepoints nest.
transaction
with_transaction on the SqlClientService in R; f’s effect may need the rest of R.
transaction_layer
The scoped transaction Layer: builds SqlTransactionService from the SqlClientService it 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 failed COMMIT surfaces.
transaction_with
transaction beginning with options, e.g. transaction_with(Isolation::Serializable, |tx| …).
with_transaction
Begin a transaction on client, run f with it, then commit on success or roll back when f fails, dies or is interrupted. A failed BEGIN or COMMIT fails with E::from(err).
with_transaction_options
with_transaction beginning with options (SqlClient::begin_with), e.g. with_transaction_options(client, Isolation::Serializable, f).

Type Aliases§

SqlTransactionEnv
The minimal typed environment holding only SqlTransactionService.