class Transaction in Drupal 9
Same name in this branch
- 9 core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction
- 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Transaction.php \Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Transaction
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Database/Transaction.php \Drupal\Core\Database\Transaction
- 10 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
- class \Drupal\Core\Database\Transaction
Expanded class hierarchy of Transaction
1 file declares its use of Transaction
- Transaction.php in core/
tests/ fixtures/ database_drivers/ module/ corefake/ src/ Driver/ Database/ corefakeWithAllCustomClasses/ Transaction.php
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\DatabaseView 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
*/
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) {
$this->connection = $connection;
// If there is no transaction depth, then no transaction has started. Name
// the transaction 'drupal_transaction'.
if (!($depth = $connection
->transactionDepth())) {
$this->name = 'drupal_transaction';
}
elseif (!$name) {
$this->name = 'savepoint_' . $depth;
}
else {
$this->name = $name;
}
$this->connection
->pushTransaction($this->name);
}
public function __destruct() {
// If we rolled back then the transaction would have already been popped.
if (!$this->rolledBack) {
$this->connection
->popTransaction($this->name);
}
}
/**
* 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 (preferable with watchdog_exception()) 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()
* @see watchdog_exception()
*/
public function rollBack() {
$this->rolledBack = TRUE;
$this->connection
->rollBack($this->name);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Transaction:: |
protected | property | The connection object for this transaction. | |
Transaction:: |
protected | property | The name of the transaction. | |
Transaction:: |
protected | property | A boolean value to indicate whether this transaction has been rolled back. | |
Transaction:: |
public | function | Retrieves the name of the transaction or savepoint. | |
Transaction:: |
public | function | Rolls back the current transaction. | |
Transaction:: |
public | function | ||
Transaction:: |
public | function |