Completed
Push — master ( 8de90d...2e89da )
by Andrii
02:52
created

Transaction::getLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\db;
9
10
use yii\helpers\Yii;
11
use yii\exceptions\InvalidConfigException;
12
use yii\exceptions\NotSupportedException;
13
14
/**
15
 * Transaction represents a DB transaction.
16
 *
17
 * It is usually created by calling [[Connection::beginTransaction()]].
18
 *
19
 * The following code is a typical example of using transactions (note that some
20
 * DBMS may not support transactions):
21
 *
22
 * ```php
23
 * $transaction = $connection->beginTransaction();
24
 * try {
25
 *     $connection->createCommand($sql1)->execute();
26
 *     $connection->createCommand($sql2)->execute();
27
 *     //.... other SQL executions
28
 *     $transaction->commit();
29
 * } catch (\Throwable $e) {
30
 *     $transaction->rollBack();
31
 *     throw $e;
32
 * }
33
 * ```
34
 *
35
 * @property bool $isActive Whether this transaction is active. Only an active transaction can [[commit()]] or
36
 * [[rollBack()]]. This property is read-only.
37
 * @property string $isolationLevel The transaction isolation level to use for this transaction. This can be
38
 * one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but also a string
39
 * containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. This property is
40
 * write-only.
41
 * @property int $level The current nesting level of the transaction. This property is read-only.
42
 *
43
 * @author Qiang Xue <[email protected]>
44
 * @since 2.0
45
 */
46
class Transaction extends \yii\base\BaseObject
47
{
48
    /**
49
     * A constant representing the transaction isolation level `READ UNCOMMITTED`.
50
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
51
     */
52
    const READ_UNCOMMITTED = 'READ UNCOMMITTED';
53
    /**
54
     * A constant representing the transaction isolation level `READ COMMITTED`.
55
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
56
     */
57
    const READ_COMMITTED = 'READ COMMITTED';
58
    /**
59
     * A constant representing the transaction isolation level `REPEATABLE READ`.
60
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
61
     */
62
    const REPEATABLE_READ = 'REPEATABLE READ';
63
    /**
64
     * A constant representing the transaction isolation level `SERIALIZABLE`.
65
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
66
     */
67
    const SERIALIZABLE = 'SERIALIZABLE';
68
69
    /**
70
     * @var Connection the database connection that this transaction is associated with.
71
     */
72
    public $db;
73
74
    /**
75
     * @var int the nesting level of the transaction. 0 means the outermost level.
76
     */
77
    private $_level = 0;
78
79
80
    public function __construct(Connection $db)
81
    {
82
        $this->db = $db;
83
    }
84
85
    /**
86
     * Returns a value indicating whether this transaction is active.
87
     * @return bool whether this transaction is active. Only an active transaction
88
     * can [[commit()]] or [[rollBack()]].
89
     */
90
    public function getIsActive()
91
    {
92
        return $this->_level > 0 && $this->db && $this->db->isActive;
93
    }
94
95
    /**
96
     * Begins a transaction.
97
     * @param string|null $isolationLevel The [isolation level][] to use for this transaction.
98
     * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but
99
     * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.
100
     * If not specified (`null`) the isolation level will not be set explicitly and the DBMS default will be used.
101
     *
102
     * > Note: This setting does not work for PostgreSQL, where setting the isolation level before the transaction
103
     * has no effect. You have to call [[setIsolationLevel()]] in this case after the transaction has started.
104
     *
105
     * > Note: Some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions
106
     * may get the same isolation level even if you did not specify any. When using this feature
107
     * you may need to set the isolation level for all transactions explicitly to avoid conflicting settings.
108
     * At the time of this writing affected DBMS are MSSQL and SQLite.
109
     *
110
     * [isolation level]: http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
111
     *
112
     * Starting from version 2.0.16, this method throws exception when beginning nested transaction and underlying DBMS
113
     * does not support savepoints.
114
     * @throws InvalidConfigException if [[db]] is `null`
115
     * @throws NotSupportedException if the DBMS does not support nested transactions
116
     * @throws Exception if DB connection fails
117
     */
118
    public function begin($isolationLevel = null)
119
    {
120
        if ($this->db === null) {
121
            throw new InvalidConfigException('Transaction::db must be set.');
122
        }
123
        $this->db->open();
124
125
        if ($this->_level === 0) {
126
            if ($isolationLevel !== null) {
127
                $this->db->getSchema()->setTransactionIsolationLevel($isolationLevel);
128
            }
129
            Yii::debug('Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : ''), __METHOD__);
130
131
            $this->db->trigger(Connection::EVENT_BEGIN_TRANSACTION);
132
            $this->db->pdo->beginTransaction();
133
            $this->_level = 1;
134
135
            return;
136
        }
137
138
        $schema = $this->db->getSchema();
139
        if ($schema->supportsSavepoint()) {
140
            Yii::debug('Set savepoint ' . $this->_level, __METHOD__);
141
            $schema->createSavepoint('LEVEL' . $this->_level);
142
        } else {
143
            Yii::info('Transaction not started: nested transaction not supported', __METHOD__);
144
            throw new NotSupportedException('Transaction not started: nested transaction not supported.');
145
        }
146
        $this->_level++;
147
    }
148
149
    /**
150
     * Commits a transaction.
151
     * @throws Exception if the transaction is not active
152
     */
153
    public function commit()
154
    {
155
        if (!$this->getIsActive()) {
156
            throw new Exception('Failed to commit transaction: transaction was inactive.');
157
        }
158
159
        $this->_level--;
160
        if ($this->_level === 0) {
161
            Yii::debug('Commit transaction', __METHOD__);
162
            $this->db->pdo->commit();
163
            $this->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
164
            return;
165
        }
166
167
        $schema = $this->db->getSchema();
168
        if ($schema->supportsSavepoint()) {
169
            Yii::debug('Release savepoint ' . $this->_level, __METHOD__);
170
            $schema->releaseSavepoint('LEVEL' . $this->_level);
171
        } else {
172
            Yii::info('Transaction not committed: nested transaction not supported', __METHOD__);
173
        }
174
    }
175
176
    /**
177
     * Rolls back a transaction.
178
     */
179
    public function rollBack()
180
    {
181
        if (!$this->getIsActive()) {
182
            // do nothing if transaction is not active: this could be the transaction is committed
183
            // but the event handler to "commitTransaction" throw an exception
184
            return;
185
        }
186
187
        $this->_level--;
188
        if ($this->_level === 0) {
189
            Yii::debug('Roll back transaction', __METHOD__);
190
            $this->db->pdo->rollBack();
191
            $this->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
192
            return;
193
        }
194
195
        $schema = $this->db->getSchema();
196
        if ($schema->supportsSavepoint()) {
197
            Yii::debug('Roll back to savepoint ' . $this->_level, __METHOD__);
198
            $schema->rollBackSavepoint('LEVEL' . $this->_level);
199
        } else {
200
            Yii::info('Transaction not rolled back: nested transaction not supported', __METHOD__);
201
        }
202
    }
203
204
    /**
205
     * Sets the transaction isolation level for this transaction.
206
     *
207
     * This method can be used to set the isolation level while the transaction is already active.
208
     * However this is not supported by all DBMS so you might rather specify the isolation level directly
209
     * when calling [[begin()]].
210
     * @param string $level The transaction isolation level to use for this transaction.
211
     * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but
212
     * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`.
213
     * @throws Exception if the transaction is not active
214
     * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
215
     */
216
    public function setIsolationLevel($level)
217
    {
218
        if (!$this->getIsActive()) {
219
            throw new Exception('Failed to set isolation level: transaction was inactive.');
220
        }
221
        Yii::debug('Setting transaction isolation level to ' . $level, __METHOD__);
222
        $this->db->getSchema()->setTransactionIsolationLevel($level);
223
    }
224
225
    /**
226
     * @return int The current nesting level of the transaction.
227
     * @since 2.0.8
228
     */
229
    public function getLevel()
230
    {
231
        return $this->_level;
232
    }
233
}
234