class Transaction (View source)

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.

Properties

protected Connection $connection

The connection object for this transaction.

protected bool $rolledBack

A boolean value to indicate whether this transaction has been rolled back.

protected string $name

The name of the transaction.

Methods

__construct(Connection $connection, $name = NULL)

No description

__destruct()

No description

name()

Retrieves the name of the transaction or savepoint.

rollBack()

Rolls back the current transaction.

Details

__construct(Connection $connection, $name = NULL)

No description

Parameters

Connection $connection
$name

__destruct()

No description

name()

Retrieves the name of the transaction or savepoint.

rollBack()

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 also

Connection::rollBack
watchdog_exception()