function Connection::addRootTransactionEndCallback

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::addRootTransactionEndCallback()
  2. 8.9.x 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.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use TransactionManagerInterface::addPostTransactionCallback() instead.

See also

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

https://www.drupal.org/node/3381002

File

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

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public function addRootTransactionEndCallback(callable $callback) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use TransactionManagerInterface::addPostTransactionCallback() instead. See https://www.drupal.org/node/3381002', E_USER_DEPRECATED);
    if ($this->transactionManager()) {
        $this->transactionManager()
            ->addPostTransactionCallback($callback);
        return;
    }
    if (!$this->transactionLayers) {
        throw new \LogicException('Root transaction end callbacks can only be added when there is an active transaction.');
    }
    $this->rootTransactionEndCallbacks[] = $callback;
}

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