【JAVA基础】事务管理

事务管理

transactionStatus使用示例


@ApiOperation("项目禁用")
@Permission(
        level = ResourceLevel.ORGANIZATION
)
@PostMapping({"/disable"})
public ResponseEntity<?> disable(@PathVariable Long organizationId, @RequestBody List<Long> idsList) {
    for (Long id : idsList) {
        TransactionStatus transactionStatus = this.dataSourceTransactionManager.getTransaction(this.transactionDefinition);
        try {
            Project project = this.projectService.selectByPrimaryKey(id);
            if (project.getProjectStatus().equals("INACTIVE")) {
                throw new CommonException("项目已禁用");
            }
            IfpOrderPost orderPost = new IfpOrderPost();
            orderPost.setProjectGid(id);
            Criteria criteria = new Criteria(orderPost);
            criteria.setWhereSql("A.ORDER_STATUS IN ('POST', 'NEW', 'PART_TENDER')");
            List<IfpOrderPost> orderPostList = this.orderPostService.selectOptional(orderPost, criteria);
            orderPostList.forEach((item) -> item.setOrderStatus("CLOSE"));
            this.orderPostService.batchUpdateOptional(orderPostList, "orderStatus");
            project.setProjectStatus("INACTIVE");
            this.projectService.updateOptional(project, "projectStatus");
            OperationRecord operationRecord;
            if (organizationId != 1) {
                operationRecord = OperationRecord.newInstance(organizationId, project.getId(), project.getXid(), "IfpProject", null, "租户禁用项目:" + project.getName());
            } else {
                operationRecord = OperationRecord.newInstance(organizationId, project.getId(), project.getXid(), "IfpProject", null, "平台禁用项目:" + project.getName());
            }
            this.operationRecordService.insertUseExecutor(operationRecord);
            this.dataSourceTransactionManager.commit(transactionStatus);
        } catch (Exception e) {
            this.dataSourceTransactionManager.rollback(transactionStatus);
            return Results.success(new HtmsResult<>(false, "操作失败:" + e.getMessage()));
        }
    }
    return Results.success(new HtmsResult<>(true, "操作成功!"));

}

image

posted on 2022-10-17 15:37  舟山婠贞  阅读(73)  评论(0编辑  收藏  举报

导航