Skip to main content

CommitHook

Trait CommitHook 

Source
pub trait CommitHook:
    Send
    + 'static
    + Sized {
    // Provided methods
    fn pre_commit(
        self,
        op: HookOperation<'_>,
    ) -> impl Future<Output = Result<PreCommitRet<'_, Self>, Error>> + Send { ... }
    fn post_commit(self) { ... }
    fn on_rollback(self) { ... }
    fn merge(&mut self, _other: &mut Self) -> bool { ... }
    fn force_execute_pre_commit(
        self,
        op: &mut impl AtomicOperation,
    ) -> impl Future<Output = Result<Self, Error>> + Send { ... }
}
Expand description

Trait for implementing custom commit hooks that execute before and after transaction commits.

Hooks execute in order: pre_commit() → database commit → post_commit(). Multiple hooks of the same type can be merged via merge().

Hooks registered on the same operation execute in registration order — see the module-level documentation for the full ordering contract and a complete example.

Provided Methods§

Source

fn pre_commit( self, op: HookOperation<'_>, ) -> impl Future<Output = Result<PreCommitRet<'_, Self>, Error>> + Send

Called before the transaction commits. Can perform database operations.

Errors returned here will roll back the transaction.

Source

fn post_commit(self)

Called after successful commit. Cannot fail, not async.

Source

fn on_rollback(self)

Called when the operation’s commit has failed after this hook’s pre_commit() had already completed successfully.

Two situations trigger it:

  1. A later hook’s pre_commit returned an error. The transaction has been rolled back before this runs — so any downstream side effect the signal triggers never contends with the failed transaction’s own locks.
  2. The COMMIT itself returned an error. The transaction is over server-side either way (it may have landed despite the client error, or aborted), so downstream side effects must be idempotent against a possibly-landed commit.

Signal-only, synchronous and infallible — mirrors post_commit(). Do not perform database work here (the transaction is gone and there is no async context); hand work to an out-of-band task via a channel send / flag set instead.

Not called when pre_commit never ran (an operation dropped without commit() produced no effects to compensate), nor for the hook whose own pre_commit failed — that hook is consumed by the failing call and must signal from its own error branch.

Source

fn merge(&mut self, _other: &mut Self) -> bool

Try to merge another hook of the same type into this one.

Returns true if merged (other will be dropped), false if not (both execute separately).

Source

fn force_execute_pre_commit( self, op: &mut impl AtomicOperation, ) -> impl Future<Output = Result<Self, Error>> + Send

Execute the hook immediately, bypassing the hook system.

Useful when AtomicOperation::add_commit_hook() returns Err(hook).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§