简体   繁体   English

抛出数据库错误异常时可以回滚事务吗?

[英]Can I rollback transaction when database error exception is thrown?

I have a large data set that needs to be written to the database when posted to the server, but it's possible that a bug in the client editor is added extra records that trigger an "integrity constraint violation". 我有一个大型数据集,在发布到服务器时需要写入数据库,但是客户端编辑器中的错误可能会添加额外的记录,从而触发“完整性约束违规”。

My problem is that when I reach the point in the data where the error is trigger, then a lot of the previous data has already been updated in the database. 我的问题是,当我到达触发错误的数据点时,数据库中的许多先前数据已经更新。 I need to rollback and reject the posted data. 我需要回滚并拒绝发布的数据。

Here's my controller's action. 这是我的控制器的动作。

/**
 * Handles saving data from the network editor.
 */
public function json_save()
{
    if($this->request->is('post'))
    {
        $result = array();
        $data = $this->request->input('json_decode');
        if(isset($data->network_id) && !empty($data->network_id))
        {
            $dataSource = $this->Node->getDataSource();
            $dataSource->begin();

            $result['nodes'] = $this->updateModel($data->network_id, $this->Node, 'nodes', $data, array(
                'ParamValue'
            ));
            $result['connections'] = $this->updateModel($data->network_id, $this->Connection, 'connections', $data);

            $dataSource->commit();

            $this->viewClass = 'Json';
            $this->set('result', $result);
            $this->set('_serialize', array(
                'result'
            ));

            return;
        }
    }

    throw new ErrorException('Posted data missing.');
}

My controller's updateModel function performs a few deletes and updates to the models $this->Node and $this->Connection . 我的控制器的updateModel函数对模型$this->Node$this->Connection执行一些删除和更新。

How do I roll back upon an "integrity constraint violation" which is usually thrown during updating of the $this->Connection model. 如何回滚在更新$this->Connection模型期间通常抛出的“完整性约束违规”。

I'm not sure if it's a PHP exception that I can catch and then do a rollback, or if there is a different way to catch it. 我不确定它是否是一个PHP异常,我可以捕获然后进行回滚,或者是否有不同的方法来捕获它。

You can do a : 你可以这样做:

$dataSource->begin();

try {
  // The queries here.
} catch (Exception $e) {
  $dataSource->rollback();
  throw $e;
}

$dataSource->commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Zend Framework数据库事务 - 无法回滚 - Zend Framework Database Transaction - Can't rollback 当在路由之外抛出异常时,如何在Slim框架中传递错误页面? - How can I deliver an error page in Slim framework when an Exception is thrown outside of a route? Symfony HTTP Client 遇到错误时,为什么无法访问响应内容,而是抛出异常? - Why can't I access the response content when the Symfony HTTP Client encounters an error, and an exception is thrown instead? 没有数据库异常发生时Laravel事务不会回滚 - Laravel transaction does not rollback when none db exception happens PHP中的数据库事务回滚处理 - database transaction rollback processing in PHP 如何通过API回滚单个Charge Stripe交易? - How can I rollback single Charge Stripe transaction via API? 在使用Datamapper / Codeigniter提交事务后,我可以回滚事务吗? - Can I rollback a transaction after commiting it with Datamapper/Codeigniter? 事务无法在codeigniter中回滚 - transaction can't rollback in codeigniter 在PHP中连接到SQL数据库时引发错误 - Error thrown when connecting to SQL database in PHP 如果抛出异常,Laravel数据库事务会发生什么? - What happens to a Laravel DB Transaction if an exception is thrown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM