首页 > 代码库 > CI框架--事务

CI框架--事务

CI框架--事务

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

1.不开启事务

1
2
3
4
//不开启事务  前两个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.自动事务

1
2
3
4
5
6
7
8
//失败自动回滚
$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.手动事务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//若失败手动回滚 成功手动提交
$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();
}
     

CI框架--事务