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(); }