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 runs_after(&self) -> &[TypeId] { ... }
    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. runs_after() lets a hook refine that order by declaring dependency types it must run after.

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 runs_after(&self) -> &[TypeId]

Hook types (by TypeId) whose still-pending instances must run their pre_commit before this hook’s.

Consulted each time this hook reaches the front of the commit pass’s queue: if any still-pending hook in the pass has a type in this list, this hook is deferred behind it and retried after other hooks execute. A listed type that never registered on the operation — or whose instances have all already executed — imposes no constraint.

Declared per instance but effectively per type: instances that merge keep the merge target’s list, so all instances of one logical hook should return the same list.

Mutually-dependent hooks (A after B and B after A, directly or transitively) cannot make progress; the commit fails loudly with a protocol error and the transaction rolls back. Never list your own type.

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§