function TransactionManagerBase::push

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

Pushes a new Drupal transaction on the stack.

This begins a client connection transaction if there is not one active, or adds a savepoint to the active one.

This method should only be called internally by a database driver.

Parameters

string $name: (optional) The name of the savepoint.

Return value

\Drupal\Core\Database\Transaction A Transaction object.

Overrides TransactionManagerInterface::push

File

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

Class

TransactionManagerBase
The database transaction manager base class.

Namespace

Drupal\Core\Database\Transaction

Code

public function push(string $name = '') : Transaction {
  if (!$this->inTransaction()) {
    // If there is no transaction active, name the transaction
    // 'drupal_transaction'.
    $name = 'drupal_transaction';
  }
  elseif (!$name) {
    // Within transactions, savepoints are used. Each savepoint requires a
    // name. So if no name is present we need to create one.
    $name = 'savepoint_' . $this->stackDepth();
  }
  if ($this->has($name)) {
    throw new TransactionNameNonUniqueException("A transaction named {$name} is already in use. Active stack: " . $this->dumpStackItemsAsString());
  }
  // Define a unique ID for the transaction.
  $id = uniqid('', TRUE);
  // Do the client-level processing.
  if ($this->stackDepth() === 0) {
    $this->beginClientTransaction();
    $type = StackItemType::Root;
    $this->setConnectionTransactionState(ClientConnectionTransactionState::Active);
    // Only set ::rootId if there's not one set already, which may happen in
    // case of broken transactions.
    if ($this->rootId === NULL) {
      $this->rootId = $id;
    }
  }
  else {
    // If we're already in a Drupal transaction then we want to create a
    // database savepoint, rather than try to begin another database
    // transaction.
    $this->addClientSavepoint($name);
    $type = StackItemType::Savepoint;
  }
  // Add an item on the stack, increasing its depth.
  $this->addStackItem($id, new StackItem($name, $type));
  // Actually return a new Transaction object.
  return new Transaction($this->connection, $name, $id);
}

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