function TransactionManagerBase::push

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

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.