# Transaction

> Transaction represents a DB transaction.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › Transaction

| class | `yii\db\Transaction` |
| --- | --- |
| Extends | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/Transaction.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/Transaction.php#L53) |

1 subclass:

- [`yii\db\pgsql\Transaction`](/docs/api/yii/db/pgsql/transaction/index.md)

Transaction represents a DB transaction.

It is usually created by calling [`Connection::beginTransaction()`](/docs/api/yii/db/connection/index.md#method-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](https://www.php.net/manual/en/class.throwable.php)
> since PHP 7.0, so you can skip the part with `\Exception` if your app uses only PHP 7.0 and higher.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-db"></span>`$db` | [`Connection`](/docs/api/yii/db/connection/index.md) | The database connection that this transaction is associated with. Default: `null`. | `Transaction` |
| [`$isActive`](#property-isActive) read-only | `bool` | Whether this transaction is active. | `Transaction` |
| [`$isolationLevel`](#property-isolationLevel) write-only | `string` | The transaction isolation level to use for this transaction. | `Transaction` |
| <span id="property-level"></span>`$level` read-only | `int` | The current nesting level of the transaction. | `Transaction` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`begin()`](#method-begin) | Begins a transaction. | `Transaction` |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`commit()`](#method-commit) | Commits a transaction. | `Transaction` |
| [`getIsActive()`](#method-getIsActive) | Returns a value indicating whether this transaction is active. | `Transaction` |
| [`getLevel()`](#method-getLevel) |  | `Transaction` |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/base/baseobject/index.md#method-init) | Initializes the object. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`rollBack()`](#method-rollBack) | Rolls back a transaction. | `Transaction` |
| [`setIsolationLevel()`](#method-setIsolationLevel) | Sets the transaction isolation level for this transaction. | `Transaction` |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-READ_UNCOMMITTED"></span>`READ_UNCOMMITTED` | `'READ UNCOMMITTED'` | A constant representing the transaction isolation level `READ UNCOMMITTED`. | `Transaction` |
| <span id="const-READ_COMMITTED"></span>`READ_COMMITTED` | `'READ COMMITTED'` | A constant representing the transaction isolation level `READ COMMITTED`. | `Transaction` |
| <span id="const-REPEATABLE_READ"></span>`REPEATABLE_READ` | `'REPEATABLE READ'` | A constant representing the transaction isolation level `REPEATABLE READ`. | `Transaction` |
| <span id="const-SERIALIZABLE"></span>`SERIALIZABLE` | `'SERIALIZABLE'` | A constant representing the transaction isolation level `SERIALIZABLE`. | `Transaction` |

## Property details

### $isActive <span id="property-isActive"></span>

```php
public bool $isActive
```

Whether this transaction is active.

Only an active transaction can
[`commit()`](/docs/api/yii/db/transaction/index.md#method-commit) or [`rollBack()`](/docs/api/yii/db/transaction/index.md#method-rollBack).

**Access**

Read-only (virtual property).

### $isolationLevel <span id="property-isolationLevel"></span>

```php
public string $isolationLevel
```

The transaction isolation level to use for this transaction.

This
can be one of [`READ_UNCOMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_UNCOMMITTED), [`READ_COMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_COMMITTED), [`REPEATABLE_READ`](/docs/api/yii/db/transaction/index.md#const-REPEATABLE_READ) and [`SERIALIZABLE`](/docs/api/yii/db/transaction/index.md#const-SERIALIZABLE) but also a
string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.

**Access**

Write-only (virtual property).

## Method details

### begin() <span id="method-begin"></span>

```php
public function begin(string|null $isolationLevel = null): mixed
```

Begins a transaction.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$isolationLevel` | `string\|null` | The [isolation level][] to use for this transaction. This can be one of [`READ_UNCOMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_UNCOMMITTED), [`READ_COMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_COMMITTED), [`REPEATABLE_READ`](/docs/api/yii/db/transaction/index.md#const-REPEATABLE_READ) and [`SERIALIZABLE`](/docs/api/yii/db/transaction/index.md#const-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: 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. > Note: The isolation level applies to the outermost transaction only. Nested transactions are implemented with savepoints and always run at the isolation level of the outer transaction, so an `$isolationLevel` passed to a nested [`begin()`](/docs/api/yii/db/transaction/index.md#method-begin) call is ignored and a warning is logged (since 2.0.56). [isolation level]: https://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels Starting from version 2.0.16, this method throws exception when beginning nested transaction and underlying DBMS does not support savepoints. |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): if [`$db`](/docs/api/yii/db/transaction/index.md#property-db) is `null`
- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): if the DBMS does not support nested transactions
- [`Exception`](/docs/api/yii/db/exception/index.md): if DB connection fails

### commit() <span id="method-commit"></span>

```php
public function commit(): mixed
```

Commits a transaction.

**Returns**

`mixed`

**Throws**

- [`Exception`](/docs/api/yii/db/exception/index.md): if the transaction is not active

### getIsActive() <span id="method-getIsActive"></span>

```php
public function getIsActive(): bool
```

Returns a value indicating whether this transaction is active.

**Returns**

`bool`: Whether this transaction is active. Only an active transaction can [`commit()`](/docs/api/yii/db/transaction/index.md#method-commit) or [`rollBack()`](/docs/api/yii/db/transaction/index.md#method-rollBack).

### getLevel() <span id="method-getLevel"></span>

```php
public function getLevel(): int
```

**Returns**

`int`: The current nesting level of the transaction.

**Since**

2.0.8

### rollBack() <span id="method-rollBack"></span>

```php
public function rollBack(): mixed
```

Rolls back a transaction.

**Returns**

`mixed`

### setIsolationLevel() <span id="method-setIsolationLevel"></span>

```php
public function setIsolationLevel(string $level): mixed
```

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()`](/docs/api/yii/db/transaction/index.md#method-begin).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$level` | `string` | The transaction isolation level to use for this transaction. This can be one of [`READ_UNCOMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_UNCOMMITTED), [`READ_COMMITTED`](/docs/api/yii/db/transaction/index.md#const-READ_COMMITTED), [`REPEATABLE_READ`](/docs/api/yii/db/transaction/index.md#const-REPEATABLE_READ) and [`SERIALIZABLE`](/docs/api/yii/db/transaction/index.md#const-SERIALIZABLE) but also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. |

**Returns**

`mixed`

**Throws**

- [`Exception`](/docs/api/yii/db/exception/index.md): if the transaction is not active

**See also**

- <https://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels>