Class yii\db\Transaction
Inheritance | yii\db\Transaction » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Available since version | 2.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
Property | Type | Description | Defined 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
Method | Description | Defined 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
Constant | Value | Description | Defined 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
The database connection that this transaction is associated with.
Whether this transaction is active. Only an active transaction can commit() or rollBack(). This property is read-only.
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.
The current nesting level of the transaction. This property is read-only.
Method Details
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
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 |
---|---|---|
throws | yii\base\NotSupportedException | if the DBMS does not support nested transactions |
throws | yii\db\Exception | if DB connection fails |
Commits a transaction.
public void commit ( ) | ||
throws | yii\db\Exception | if the transaction is not active |
---|
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(). |
---|
public integer getLevel ( ) | ||
return | integer | The current nesting level of the transaction. |
---|
Rolls back a transaction.
public void rollBack ( ) |
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 |
throws | yii\db\Exception | if the transaction is not active |
---|
Signup or Login in order to comment.