1 | <?php |
||
45 | class Transaction extends \yii\db\Transaction |
||
46 | { |
||
47 | |||
48 | /** |
||
49 | * @var integer the nesting level of the transaction. 0 means the outermost level. |
||
50 | */ |
||
51 | private $_level = 0; |
||
52 | |||
53 | /** |
||
54 | * Returns a value indicating whether this transaction is active. |
||
55 | * @return boolean whether this transaction is active. Only an active transaction |
||
56 | * can [[commit()]] or [[rollBack()]]. |
||
57 | */ |
||
58 | 6 | public function getIsActive() |
|
62 | |||
63 | /** |
||
64 | * Begins a transaction. |
||
65 | * @param string|null $isolationLevel The [isolation level][] to use for this transaction. |
||
66 | * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but |
||
67 | * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. |
||
68 | * If not specified (`null`) the isolation level will not be set explicitly and the DBMS default will be used. |
||
69 | * |
||
70 | * > Note: This setting does not work for PostgreSQL, where setting the isolation level before the transaction |
||
71 | * has no effect. You have to call [[setIsolationLevel()]] in this case after the transaction has started. |
||
72 | * |
||
73 | * > Note: Some DBMS allow setting of the isolation level only for the whole connection so subsequent transactions |
||
74 | * may get the same isolation level even if you did not specify any. When using this feature |
||
75 | * you may need to set the isolation level for all transactions explicitly to avoid conflicting settings. |
||
76 | * At the time of this writing affected DBMS are MSSQL and SQLite. |
||
77 | * |
||
78 | * [isolation level]: http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
79 | * @throws InvalidConfigException if [[db]] is `null`. |
||
80 | */ |
||
81 | 6 | public function begin($isolationLevel = null) |
|
82 | { |
||
83 | 6 | if ($this->db === null) { |
|
84 | throw new InvalidConfigException('Transaction::db must be set.'); |
||
85 | } |
||
86 | 6 | $this->db->open(); |
|
87 | |||
88 | 6 | if ($this->_level == 0) { |
|
89 | 6 | Yii::trace('Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : ''), __METHOD__); |
|
|
|||
90 | |||
91 | 6 | $this->db->trigger(Connection::EVENT_BEGIN_TRANSACTION); |
|
92 | 6 | if ($isolationLevel !== null) { |
|
93 | 2 | $this->db->pdo->beginTransaction(false); |
|
94 | 2 | $this->db->getSchema()->setTransactionIsolationLevel($isolationLevel); |
|
95 | } else { |
||
96 | 4 | $this->db->pdo->beginTransaction(); |
|
97 | } |
||
98 | 6 | $this->_level = 1; |
|
99 | |||
100 | 6 | return; |
|
101 | } |
||
102 | |||
103 | 1 | $schema = $this->db->getSchema(); |
|
104 | 1 | if ($schema->supportsSavepoint()) { |
|
105 | 1 | Yii::trace('Set savepoint ' . $this->_level, __METHOD__); |
|
106 | 1 | $schema->createSavepoint('LEVEL' . $this->_level); |
|
107 | } else { |
||
108 | Yii::info('Transaction not started: nested transaction not supported', __METHOD__); |
||
109 | } |
||
110 | 1 | $this->_level++; |
|
111 | 1 | } |
|
112 | |||
113 | /** |
||
114 | * Commits a transaction. |
||
115 | * @throws Exception if the transaction is not active |
||
116 | */ |
||
117 | 5 | public function commit() |
|
118 | { |
||
119 | 5 | if (!$this->getIsActive()) { |
|
120 | throw new Exception('Failed to commit transaction: transaction was inactive.'); |
||
121 | } |
||
122 | |||
123 | 5 | $this->_level--; |
|
124 | 5 | if ($this->_level == 0) { |
|
125 | 5 | Yii::trace('Commit transaction', __METHOD__); |
|
126 | 5 | $this->db->pdo->commit(); |
|
127 | 5 | $this->db->trigger(Connection::EVENT_COMMIT_TRANSACTION); |
|
128 | 5 | return; |
|
129 | } |
||
130 | |||
131 | $schema = $this->db->getSchema(); |
||
132 | if ($schema->supportsSavepoint()) { |
||
133 | Yii::trace('Release savepoint ' . $this->_level, __METHOD__); |
||
134 | $schema->releaseSavepoint('LEVEL' . $this->_level); |
||
135 | } else { |
||
136 | Yii::info('Transaction not committed: nested transaction not supported', __METHOD__); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Rolls back a transaction. |
||
142 | * @throws Exception if the transaction is not active |
||
143 | */ |
||
144 | 3 | public function rollBack() |
|
145 | { |
||
146 | 3 | if (!$this->getIsActive()) { |
|
147 | // do nothing if transaction is not active: this could be the transaction is committed |
||
148 | // but the event handler to "commitTransaction" throw an exception |
||
149 | return; |
||
150 | } |
||
151 | |||
152 | 3 | $this->_level--; |
|
153 | 3 | if ($this->_level == 0) { |
|
154 | 2 | Yii::trace('Roll back transaction', __METHOD__); |
|
155 | 2 | $this->db->pdo->rollBack(); |
|
156 | 2 | $this->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION); |
|
157 | 2 | return; |
|
158 | } |
||
159 | |||
160 | 1 | $schema = $this->db->getSchema(); |
|
161 | 1 | if ($schema->supportsSavepoint()) { |
|
162 | 1 | Yii::trace('Roll back to savepoint ' . $this->_level, __METHOD__); |
|
163 | 1 | $schema->rollBackSavepoint('LEVEL' . $this->_level); |
|
164 | } else { |
||
165 | Yii::info('Transaction not rolled back: nested transaction not supported', __METHOD__); |
||
166 | // throw an exception to fail the outer transaction |
||
167 | throw new Exception('Roll back failed: nested transaction not supported.'); |
||
168 | } |
||
169 | 1 | } |
|
170 | |||
171 | /** |
||
172 | * Sets the transaction isolation level for this transaction. |
||
173 | * |
||
174 | * This method can be used to set the isolation level while the transaction is already active. |
||
175 | * However this is not supported by all DBMS so you might rather specify the isolation level directly |
||
176 | * when calling [[begin()]]. |
||
177 | * @param string $level The transaction isolation level to use for this transaction. |
||
178 | * This can be one of [[READ_UNCOMMITTED]], [[READ_COMMITTED]], [[REPEATABLE_READ]] and [[SERIALIZABLE]] but |
||
179 | * also a string containing DBMS specific syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. |
||
180 | * @throws Exception if the transaction is not active |
||
181 | * @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
||
182 | */ |
||
183 | public function setIsolationLevel($level) |
||
191 | |||
192 | /** |
||
193 | * @return integer The current nesting level of the transaction. |
||
194 | * @since 2.0.8 |
||
195 | */ |
||
196 | 4 | public function getLevel() |
|
200 | } |
||
201 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.