springboot 事务的使用

@Transactional
public JsonUtil secondInstoreConfirm(@RequestParam(value = "taskId") String taskId) {
JsonUtil json = JsonUtil.newInstance();
try {
Task task = taskService.createTaskQuery().taskId(taskId).includeProcessVariables().singleResult();
if (task == null)
return JsonUtil.error("审批任务未找到");
String userId = getCurrentUserId();
//测试数据
// userId = "2BDE7CB62A438BEF7EC73ADE194B44CB";
if (task.getAssignee().equals(userId) == false)
return JsonUtil.error("对不起,您没有审批权限");

//出库
List<StockUpdate> stockUpdateEntityList = getInStoreApplyEntityForm(taskId);
//执行仓库的出库操作
json= outWarehouseService.outBound(stockUpdateEntityList, userId);
if(json.getCode()!=0)
{
throw new RuntimeException();
// return json;
}
Map<String, Object> map = new HashMap<String, Object>();
// map.put("stockUpdateEntityList", stockUpdateEntityList);

System.out.println("开始入库");

// String secondStoreId = storeRecordDao.findRecordIdByTicketAndLevel(userId, "二级");
String secondStoreId =(String)task.getProcessVariables().get("needInStoreroomId");

String outNo = (String) taskService.getVariable(taskId, "outNo");
//把仓库变成需要入库的二级库
for (int i = 0; i < stockUpdateEntityList.size(); i++) {
stockUpdateEntityList.get(i).setStoreroomId(secondStoreId);
}
boolean rst = enterWarehouseService.notLevelOneStorage(stockUpdateEntityList, userId, outNo);
if(rst==false){
throw new RuntimeException();
// return JsonUtil.error(StockConstants.SERVER_ERROR_TIP);
}

// map.put("inStockEntityList", stockUpdateEntityList);
taskService.complete(taskId, map);

notifyApplyerExt( task,"确认入库完成");

return JsonUtil.success("二级仓库管理员确认入库完成");
} catch (Exception e) {
e.printStackTrace();
throw e;
}

}

这个函数,包括两步骤,一级库出库,二级库入库。这是一个事务操作。一旦发生异常需要回滚。
(1)需要在catch分支中抛出异常。一旦发生异常。就会回滚。
(2)try分支中,原来出错判断,是执行return json,现在改为抛出异常。进入catch分支。然后throw e,然后@Tranactional就会发挥作用。
是不是很简单!!!




猜你喜欢

转载自www.cnblogs.com/zhanying999666/p/9667523.html