You are here

public function Connection::addRootTransactionEndCallback in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::addRootTransactionEndCallback()

Adds a root transaction end callback.

These callbacks are invoked immediately after the transaction has been committed.

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.

An argument is passed to the callbacks that indicates whether the transaction was successful or not.

Parameters

callable $callback: The callback to invoke.

See also

\Drupal\Core\Database\Connection::doCommit()

File

core/lib/Drupal/Core/Database/Connection.php, line 1268

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

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