Commit 4969f51e by Qiang Xue

Fixes #3221: Added events for DB transaction commit/rollback

parent 838a79cd
......@@ -81,6 +81,7 @@ Yii Framework 2 Change Log
- Enh #3154: Added validation error display for `GridView` filters (ivan-kolmychek)
- Enh #3196: Masked input upgraded to use jquery.inputmask plugin with more features. (kartik-v)
- Enh #3220: Added support for setting transaction isolation levels (cebe)
- Enh #3221: Added events for DB transaction commit/rollback (drcypher, qiangxue)
- Enh #3222: Added `useTablePrefix` option to the model generator for Gii (horizons2)
- Enh #3230: Added `yii\filters\AccessControl::user` to support access control with different actors (qiangxue)
- Enh #3232: Added `export()` and `exportAsString()` methods to `yii\helpers\BaseVarDumper` (klimov-paul)
......
......@@ -107,6 +107,18 @@ class Connection extends Component
* @event Event an event that is triggered after a DB connection is established
*/
const EVENT_AFTER_OPEN = 'afterOpen';
/**
* @event Event an event that is triggered right before a top-level transaction is started
*/
const EVENT_BEGIN_TRANSACTION = 'beginTransaction';
/**
* @event Event an event that is triggered right after a top-level transaction is committed
*/
const EVENT_COMMIT_TRANSACTION = 'commitTransaction';
/**
* @event Event an event that is triggered right after a top-level transaction is rolled back
*/
const EVENT_ROLLBACK_TRANSACTION = 'rollbackTransaction';
/**
* @var string the Data Source Name, or DSN, contains the information required to connect to the database.
......
......@@ -113,6 +113,8 @@ class Transaction extends \yii\base\Object
$this->db->getSchema()->setTransactionIsolationLevel($isolationLevel);
}
Yii::trace('Begin transaction' . ($isolationLevel ? ' with isolation level ' . $isolationLevel : ''), __METHOD__);
$this->db->trigger(Connection::EVENT_BEGIN_TRANSACTION);
$this->db->pdo->beginTransaction();
$this->_level = 1;
......@@ -143,7 +145,7 @@ class Transaction extends \yii\base\Object
if ($this->_level == 0) {
Yii::trace('Commit transaction', __METHOD__);
$this->db->pdo->commit();
$this->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
return;
}
......@@ -163,14 +165,16 @@ class Transaction extends \yii\base\Object
public function rollBack()
{
if (!$this->getIsActive()) {
throw new Exception('Failed to roll back transaction: transaction was inactive.');
// do nothing if transaction is not active: this could be the transaction is committed
// but the event handler to "commitTransaction" throw an exception
return;
}
$this->_level--;
if ($this->_level == 0) {
Yii::trace('Roll back transaction', __METHOD__);
$this->db->pdo->rollBack();
$this->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
return;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment