gpt4 book ai didi

java - 添加了@Tansactional,但是显式抛出检查异常时回滚仍然没有发生

转载 作者:行者123 更新时间:2023-12-01 18:35:15 25 4
gpt4 key购买 nike

@Transactional(value="txManager", rollbackFor = DriverReassignmentException.class)
public UserLoadAssocResponse<List<AssociateResponse>, UserLoadAssocResponseStatus> reassignAssignedLoad(String user, String scac, String adminId, DriverLoadAssocDetails driverLoadAssocDetails) throws DriverReassignmentException{

UserLoadAssocResponse<Boolean, UserLoadAssocResponseStatus> removeUserResponse =null;

UserLoadAssocResponse<List<AssociateResponse>, UserLoadAssocResponseStatus> userLoadAssignedResponse=null;

if(!(StringUtils.isEmpty(driverLoadAssocDetails.getOrderNumber()) && StringUtils.isEmpty(user) && StringUtils.isEmpty(scac) && StringUtils.isEmpty(adminId))){
try {
removeUserResponse = this.userLoadAssociationClient.removeUserFromAssignedLoad(user, scac, driverLoadAssocDetails.getOrderNumber(),adminId, Constant.INTRANSIT);
} catch (Exception e) {
removeUserResponse = UserLoadAssocResponse.failure(UserLoadAssocResponseStatus.UNEXPECTED_ERROR);
log.error("Unable to remove user {} from load {} scac: {} adminUser: {} admin", user, driverLoadAssocDetails.getOrderNumber(), scac, adminId, e);
}
} else{
log.error("Insufficient data present to remove user {} from load {} scac: {} adminUser: {} admin", user, driverLoadAssocDetails.getOrderNumber()!=null?driverLoadAssocDetails.getOrderNumber():null, scac, adminId);
return UserLoadAssocResponse.failure(UserLoadAssocResponseStatus.UNEXPECTED_ERROR);

}

if(removeUserResponse.isSuccess() && removeUserResponse.getStatus().toString().equalsIgnoreCase(UserLoadAssocResponseStatus.SUCCESS.toString())){
try {
userLoadAssignedResponse = this.associateSingleUserSingleLoad(scac, adminId, driverLoadAssocDetails);
}catch(Exception ex){
log.error("Unexpected error occured while assigning single load for carrierCode {} - {}", scac, ex);
throw new DriverReassignmentException(UserLoadAssocResponseStatus.UNEXPECTED_ERROR.name());
}
}else{
return UserLoadAssocResponse.failure(removeUserResponse.getStatus());
}

if(userLoadAssignedResponse.isSuccess() && userLoadAssignedResponse.getStatus().toString().equalsIgnoreCase(UserLoadAssocResponseStatus.SUCCESS.toString())){
log.info("Load reassigned succesfully:{}", userLoadAssignedResponse);
return userLoadAssignedResponse;
}else{
log.error("Error occured while assigning single load for carrierCode {}", scac);
throw new DriverReassignmentException("Load reassigned Unsuccesfull");

}

}
// expecting to rollback this.userLoadAssociationClient.removeUserFromAssignedLoad(user, scac, driverLoadAssocDetails.getOrderNumber(),adminId, Constant.INTRANSIT) 

抛出 new DriverReassignmentException("Load resigned Unsuccesfull") 时。但不是可以实现的

最佳答案

可能的实际错误

删除以下行中的 return 语句:

return UserLoadAssocResponse.failure

我认为您从该方法返回时没有评估会抛出正确异常的 if 条件。

额外的调试

你的逻辑有问题。真正调试事务注释是否正常工作的最佳方法是将以下行添加为方法的最后一行但不带 if 语句

throw new DriverReassignmentException("Load reassigned Unsuccessful");

然后您将能够评估是否发生回滚,因为您将知道对于每个请求,您都会抛出特定的异常。

然后,注释掉抛出,您应该注意到您的数据已保存,并且没有发生异常。

问题出在if 语句的条件上。您需要调试逻辑并了解为什么 if 语句 的计算结果不为 true。

关于java - 添加了@Tansactional,但是显式抛出检查异常时回滚仍然没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60072332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com