ceallan 2015-05-07 16:53:57 22335次浏览 7条回复 16 11 0

这里说一下关于使用数据库事务几项功能的改进。

首先,你现在可以像下面这样以回调形式的事务工作:

$connection->transaction(function() {
    $order = new Order($customer);
    $order->save();
    $order->addItems($items);
});

这相当于下列冗长的代码:

$transaction = $connection->beginTransaction();
try {
    $order = new Order($customer);
    $order->save();
    $order->addItems($items);
    $transaction->commit();
} catch (\Exception $e) {
    $transaction->rollBack();
    throw $e;
}

其它,事务可以触发一系列事件。 例如, a beginTransaction event is triggered by a DB connection when you start a new transaction; and a commitTransaction event is triggered when the transaction is successfully committed. You can respond to these events to perform some preprocessing and post-processing tasks when using transactions.

最后,开始一个新的事务时,您可以设置事务隔离级别(例如READ COMMITTED)。例如,

$transaction = $connection->beginTransaction(Transaction::READ_COMMITTED);
觉得很赞
您需要登录后才可以回复。登录 | 立即注册