CI框架--事务

CI框架中事务封装的很完善,使用起来很简单

1.不开启事务

//不开启事务  前两个sql 能够执行成功,第三个执行失败
$this->device_model->addForCamera(1, '爱三123', '2016122903', 0);
$this->device_model->setStateInDevices(1, array(2));
$this->device_model->addForCamera(1, '爱三1234567890', '2016122903', 0);

2.自动事务

//失败自动回滚
$this->db->trans_start();
//三条sql 都执行失败
$this->device_model->addForCamera(1, '爱三123', '2016122903', 0);
$this->device_model->setStateInDevices(1, array(2));
$this->device_model->addForCamera(1, '爱三1234567890', '2016122903', 0); $this->db->trans_complete();

3.手动事务

//若失败手动回滚 成功手动提交
$this->db->trans_begin(); //三条sql 都执行失败
$this->device_model->addForCamera(1, '爱三123', '2016122903', 0);
$this->device_model->setStateInDevices(1, array(2));
$this->device_model->addForCamera(1, '爱三1234567890', '2016122903', 0); if ($this->db->trans_status() === false) {
$this->db->trans_rollback();
} else {
$this->db->trans_commit();
}

  

  

上一篇:python三级菜单实例(傻瓜版和进阶版)


下一篇:HihoCoder 1067 最近公共祖先(ST离线算法)