今天测试“审批流撤销”功能,突然遇到一个奇怪的问题:日志显示新增数据成功,db里却查不到。
代码如下:
try{
$service_document->retreat($workflow_type_id, $doc_manage_id);
$this->requestSuccessResponse();
}catch(\Exception $e){
$service_log = new LogService();
$service_log->createLog($doc_manage_id, LabelService::TEXT_LOG_CATEGORY_DOCUMENT, $e->getMessage());
$this->requestErrorResponse("retreat fail");
}
createLog是添加日志记录的方法。测试功能,提示了retreat fail,但是数据库日志表里却看不到数据。
新增日志的方法,打印出sql以及新增之后的主键ID,都是没问题的。
问题出在Service里retreat方法上。先上代码:
$res3 = $obj_model->table('tms_new_workflow_process')
->where(array('workflow_type_id' => $type, 'subject_id'=> $doc_manage_id))
->save($process_update);
if(false === $res3){
$obj_model->rollback();
throw new Exception("update workflow process fail!");
}
$obj_model->commit();
就是当save出错时,底层会抛异常Exception,直接被控制器里捕获catch到,从而导致上面代码里事物的回退rollback根本就没执行。
在Service里retreat整个方法里,加上try-catch,这样就不会漏掉一些底部的异常啦。
try{
...... //其它任务
$res3 = $obj_model->table('tms_new_workflow_process')
->where(array('workflow_type_id' => $type, 'subject_id'=> $doc_manage_id))
->save($process_update);
if(false === $res3){
throw new Exception("update workflow process fail!");
}
}catch(\Exception $e){
$obj_model->rollback();
}
$obj_model->commit();
所以遇到类似问题,就检查你的sql事务有没有正常结束哦!