class Transaction

Same name in this branch
  1. main core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Transaction.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Transaction
Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Transaction.php \Drupal\driver_test\Driver\Database\DrivertestMysql\Transaction
  2. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Transaction.php \Drupal\driver_test\Driver\Database\DrivertestPgsql\Transaction
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Transaction.php \Drupal\Core\Database\Driver\sqlite\Transaction
  4. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Transaction.php \Drupal\Core\Database\Driver\mysql\Transaction
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Transaction.php \Drupal\Core\Database\Driver\pgsql\Transaction
  6. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Transaction.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Transaction
  7. 11.x core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction
  8. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Transaction.php \Drupal\core_fake\Driver\Database\CoreFakeWithAllCustomClasses\Transaction
  9. 10 core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction
  10. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Transaction.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Transaction
  11. 9 core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction
  12. 8.9.x core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction

A wrapper class for creating and managing database transactions.

In the vast majority of cases, you should not instantiate this class directly. Instead, call ->startTransaction(), from the appropriate connection object.

Hierarchy

Expanded class hierarchy of Transaction

See also

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

6 files declare their use of Transaction
Connection.php in core/tests/fixtures/database_drivers/custom/fake/Connection.php
DriverSpecificTransactionTestBase.php in core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php
Transaction.php in core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Transaction.php
TransactionManagerBase.php in core/lib/Drupal/Core/Database/Transaction/TransactionManagerBase.php
TransactionManagerInterface.php in core/lib/Drupal/Core/Database/Transaction/TransactionManagerInterface.php

... See full list

1 string reference to 'Transaction'
ConnectionTest::providerGetDriverClass in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
Data provider for testGetDriverClass().

File

core/lib/Drupal/Core/Database/Transaction.php, line 14

Namespace

Drupal\Core\Database
View source
class Transaction {
  public function __construct(protected readonly Connection $connection, protected readonly string $name, protected readonly string $id) {
    // Transactions rely on objects being destroyed in order to be committed.
    // PHP makes no guarantee about the order in which objects are destroyed so
    // ensure all transactions are committed on shutdown.
    // @todo remove in drupal:13.0.0.
    // @phpstan-ignore staticMethod.deprecated
    Database::commitAllOnShutdown();
  }
  
  /**
   * Destructs the object.
   *
   * If the transaction is still active at this stage, and depending on the
   * state of the transaction stack, this leads to a COMMIT (for a root item)
   * or to a RELEASE SAVEPOINT (for a savepoint item) executed on the database.
   */
  public function __destruct() {
    $this->connection
      ->transactionManager()
      ->purge($this->name, $this->id);
  }
  
  /**
   * Prevent transactions from being unserialized.
   */
  public function __wakeup() : void {
    throw new \BadMethodCallException('Cannot unserialize ' . get_class($this));
  }
  
  /**
   * Retrieves the name of the transaction or savepoint.
   */
  public function name() {
    return $this->name;
  }
  
  /**
   * Returns the transaction to the parent nesting level.
   *
   * Depending on the state of the transaction stack, this leads to a COMMIT
   * operation (for a root item), or to a RELEASE SAVEPOINT operation (for a
   * savepoint item) executed on the database.
   */
  public function commitOrRelease() : void {
    $this->connection
      ->transactionManager()
      ->unpile($this->name, $this->id);
  }
  
  /**
   * Rolls back the transaction.
   *
   * Depending on the state of the transaction stack, this leads to a ROLLBACK
   * operation (for a root item), or to a ROLLBACK TO SAVEPOINT + a RELEASE
   * SAVEPOINT operations (for a savepoint item) executed on the database.
   */
  public function rollBack() {
    $this->connection
      ->transactionManager()
      ->rollback($this->name, $this->id);
  }

}

Members

Title Sort descending Modifiers Object type Summary
Transaction::commitOrRelease public function Returns the transaction to the parent nesting level.
Transaction::name public function Retrieves the name of the transaction or savepoint.
Transaction::rollBack public function Rolls back the transaction.
Transaction::__construct public function
Transaction::__destruct public function Destructs the object.
Transaction::__wakeup public function Prevent transactions from being unserialized.

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