function TransactionManagerBase::addPostTransactionCallback

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Database/Transaction/TransactionManagerBase.php \Drupal\Core\Database\Transaction\TransactionManagerBase::addPostTransactionCallback()

Adds a root transaction end callback.

It can for example be used to avoid deadlocks on write-heavy tables that do not need to be part of the transaction, like cache tag invalidations.

Another use case is that services using alternative backends like Redis and Memcache cache implementations can replicate the transaction-behavior of the database cache backend and avoid race conditions.

These callbacks are invoked during the destruction of the root Transaction object.

The callback should have the following signature:


  callback(
    bool $success,
  ): void

When callbacks are executed, the $success parameter passed to the callbacks is a boolean that indicates

  • if TRUE, that the complete transaction was successfully committed, or in the edge case of a transaction that was auto-committed after a DDL statement, that no rollbacks were attempted after the DDL statement;
  • if FALSE, that the complete transaction was rolled back, or that the transaction processing failed for any other reason.

Parameters

callable $callback: The callback to invoke.

Overrides TransactionManagerInterface::addPostTransactionCallback

File

core/lib/Drupal/Core/Database/Transaction/TransactionManagerBase.php, line 471

Class

TransactionManagerBase
The database transaction manager base class.

Namespace

Drupal\Core\Database\Transaction

Code

public function addPostTransactionCallback(callable $callback) : void {
  if (!$this->inTransaction()) {
    throw new \LogicException('Root transaction end callbacks can only be added when there is an active transaction.');
  }
  $this->postTransactionCallbacks[] = $callback;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.