Class yii\db\Transaction

Inheritanceyii\db\Transaction » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/db/Transaction.php

Transaction represents a DB transaction.

It is usually created by calling yii\db\Connection::beginTransaction().

The following code is a typical example of using transactions (note that some DBMS may not support transactions):

$transaction = $connection->beginTransaction();
try {
    $connection->createCommand($sql1)->execute();
    $connection->createCommand($sql2)->execute();
    //.... other SQL executions
    $transaction->commit();
} catch (\Exception $e) {
    $transaction->rollBack();
    throw $e;
} catch (\Throwable $e) {
    $transaction->rollBack();
    throw $e;
}

Note: in the above code we have two catch-blocks for compatibility with PHP 5.x and PHP 7.x. \Exception implements the \Throwable interface since PHP 7.0, so you can skip the part with \Exception if your app uses only PHP 7.0 and higher.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$db yii\db\Connection The database connection that this transaction is associated with. yii\db\Transaction
$isActive boolean Whether this transaction is active. Only an active transaction can commit() or rollBack(). This property is read-only. yii\db\Transaction
$isolationLevel string The transaction isolation level to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL. This property is write-only. yii\db\Transaction
$level integer The current nesting level of the transaction. This property is read-only. yii\db\Transaction

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
begin() Begins a transaction. yii\db\Transaction
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
commit() Commits a transaction. yii\db\Transaction
getIsActive() Returns a value indicating whether this transaction is active. yii\db\Transaction
getLevel() yii\db\Transaction
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
rollBack() Rolls back a transaction. yii\db\Transaction
setIsolationLevel() Sets the transaction isolation level for this transaction. yii\db\Transaction

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
READ_COMMITTED 'READ COMMITTED' A constant representing the transaction isolation level READ COMMITTED. yii\db\Transaction
READ_UNCOMMITTED 'READ UNCOMMITTED' A constant representing the transaction isolation level READ UNCOMMITTED. yii\db\Transaction
REPEATABLE_READ 'REPEATABLE READ' A constant representing the transaction isolation level REPEATABLE READ. yii\db\Transaction
SERIALIZABLE 'SERIALIZABLE' A constant representing the transaction isolation level SERIALIZABLE. yii\db\Transaction

Property Details

$db public property

The database connection that this transaction is associated with.

public yii\db\Connection $db null
$isActive public property

Whether this transaction is active. Only an active transaction can commit() or rollBack(). This property is read-only.

public boolean $isActive null
$isolationLevel public property

The transaction isolation level to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL. This property is write-only.

public string $isolationLevel null
$level public property

The current nesting level of the transaction. This property is read-only.

public integer $level null

Method Details

begin() public method

Begins a transaction.

public void begin ( $isolationLevel null )
$isolationLevel string|null

The isolation level to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL. If not specified (null) the isolation level will not be set explicitly and the DBMS default will be used.

Note: This setting does not work for PostgreSQL, where setting the isolation level before the transaction has no effect. You have to call setIsolationLevel() in this case after the transaction has started.

Note: Some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions may get the same isolation level even if you did not specify any. When using this feature you may need to set the isolation level for all transactions explicitly to avoid conflicting settings. At the time of this writing affected DBMS are MSSQL and SQLite.

Starting from version 2.0.16, this method throws exception when beginning nested transaction and underlying DBMS does not support savepoints.

throws yii\base\InvalidConfigException

if $db is null

throws yii\base\NotSupportedException

if the DBMS does not support nested transactions

throws yii\db\Exception

if DB connection fails

commit() public method

Commits a transaction.

public void commit ( )
throws yii\db\Exception

if the transaction is not active

getIsActive() public method

Returns a value indicating whether this transaction is active.

public boolean getIsActive ( )
return boolean

Whether this transaction is active. Only an active transaction can commit() or rollBack().

getLevel() public method (available since version 2.0.8)

public integer getLevel ( )
return integer

The current nesting level of the transaction.

rollBack() public method

Rolls back a transaction.

public void rollBack ( )
setIsolationLevel() public method

Sets the transaction isolation level for this transaction.

This method can be used to set the isolation level while the transaction is already active. However this is not supported by all DBMS so you might rather specify the isolation level directly when calling begin().

See also http://en.wikipedia.org/wiki/Isolation_(database_systems)#Isolation_levels.

public void setIsolationLevel ( $level )
$level string

The transaction isolation level to use for this transaction. This can be one of READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE but also a string containing DBMS specific syntax to be used after SET TRANSACTION ISOLATION LEVEL.

throws yii\db\Exception

if the transaction is not active