class Transaction

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

A wrapper class for creating and managing database transactions.

Not all databases or database configurations support transactions. For example, MySQL MyISAM tables do not. It is also easy to begin a transaction and then forget to commit it, which can lead to connection errors when another transaction is started.

This class acts as a wrapper for transactions. To begin a transaction, simply instantiate it. When the object goes out of scope and is destroyed it will automatically commit. It also will check to see if the specified connection supports transactions. If not, it will simply skip any transaction commands, allowing user-space code to proceed normally. The only difference is that rollbacks won't actually do anything.

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

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
StubConnection.php in core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.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

... See full list

3 string references to 'Transaction'
Connection::getDriverClass in core/lib/Drupal/Core/Database/Connection.php
Gets the driver-specific override class if any for the specified class.
Connection::startTransaction in core/lib/Drupal/Core/Database/Connection.php
Returns a new DatabaseTransaction object on this connection.
ConnectionTest::providerGetDriverClass in core/tests/Drupal/Tests/Core/Database/ConnectionTest.php
Data provider for testGetDriverClass().

File

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

Namespace

Drupal\Core\Database
View source
class Transaction {
    
    /**
     * The connection object for this transaction.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $connection;
    
    /**
     * A boolean value to indicate whether this transaction has been rolled back.
     *
     * @var bool
     *
     * @deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. There is
     *   no replacement.
     *
     * @see https://www.drupal.org/node/3381002
     */
    protected $rolledBack = FALSE;
    
    /**
     * The name of the transaction.
     *
     * This is used to label the transaction savepoint. It will be overridden to
     * 'drupal_transaction' if there is no transaction depth.
     *
     * @var string
     */
    protected $name;
    public function __construct(Connection $connection, $name = NULL, 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.
        Database::commitAllOnShutdown();
        if ($connection->transactionManager()) {
            $this->connection = $connection;
            $this->name = $name;
            return;
        }
        // Start of BC layer.
        $this->connection = $connection;
        // If there is no transaction depth, then no transaction has started. Name
        // the transaction 'drupal_transaction'.
        // @phpstan-ignore-next-line
        if (!($depth = $connection->transactionDepth())) {
            $this->name = 'drupal_transaction';
        }
        elseif (!$name) {
            $this->name = 'savepoint_' . $depth;
        }
        else {
            $this->name = $name;
        }
        // @phpstan-ignore-next-line
        $this->connection
            ->pushTransaction($this->name);
        // End of BC layer.
    }
    public function __destruct() {
        if ($this->connection
            ->transactionManager()) {
            $this->connection
                ->transactionManager()
                ->unpile($this->name, $this->id);
            return;
        }
        // Start of BC layer.
        // If we rolled back then the transaction would have already been popped.
        // @phpstan-ignore-next-line
        if (!$this->rolledBack) {
            // @phpstan-ignore-next-line
            $this->connection
                ->popTransaction($this->name);
        }
        // End of BC layer.
    }
    
    /**
     * Retrieves the name of the transaction or savepoint.
     */
    public function name() {
        return $this->name;
    }
    
    /**
     * Rolls back the current transaction.
     *
     * This is just a wrapper method to rollback whatever transaction stack we are
     * currently in, which is managed by the connection object itself. Note that
     * logging needs to happen after a transaction has been rolled back or the log
     * messages will be rolled back too.
     *
     * @see \Drupal\Core\Database\Connection::rollBack()
     */
    public function rollBack() {
        if ($this->connection
            ->transactionManager()) {
            $this->connection
                ->transactionManager()
                ->rollback($this->name, $this->id);
            return;
        }
        // Start of BC layer.
        // @phpstan-ignore-next-line
        $this->rolledBack = TRUE;
        // @phpstan-ignore-next-line
        $this->connection
            ->rollBack($this->name);
        // End of BC layer.
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
Transaction::$connection protected property The connection object for this transaction.
Transaction::$name protected property The name of the transaction.
Transaction::$rolledBack Deprecated protected property A boolean value to indicate whether this transaction has been rolled back.
Transaction::name public function Retrieves the name of the transaction or savepoint.
Transaction::rollBack public function Rolls back the current transaction.
Transaction::__construct public function
Transaction::__destruct public function

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