Transaction rolled back because it has been marked as rollback-only 原因 和解决方案
产生原因 ,
1 serviceA 调用 serviceB 然后 B 抛出异常 ,B 所在的 事物 回滚,B 把当前可写 事物标记成 只读事物 ,
2 如果 A 和B 是在 同一个事物环境,并且 A 抓了 B 抛出的异常,没有和 B 一起回滚,
3 然后 A 方法 完成,把当前事物 当成 写事物提交。就会出上面的问题。
上代码:


解释: 可以看出 上面代码 问题有点多, 方法 A 是 没有 事物环境的,也就是说 调用方法A 的 前面的方法如果有事物环境, 方法A 就 依赖前面的事物环境, 没有 方法A 就 是以非事物的方式执行( 级联操作 ,如果 在非事物 环境执行,就不能级联回滚,正常必须要在事物环境 ),然后 就是 随意的抓异常,让本来应该回滚的 事物没有回滚,依旧在向下执行。
正确的处理方法: 如果是级联操作,那么 应该处于同一个 事物环境,并且不应该 随意的抓异常,只有 自己 能处理 ,并且不影响事物回滚的异常才能抓,别的异常统统不允许抓。并且 默认的 回滚需要 runtimexception ,所以只能抛出这个异常的子类 ,如果 出现别的异常, 非 runtimexception 那么 应该抓了 抛出 runtimexception 。
另外 说一句 spring有完善的全局 异常处理体系, 正常来说不需要 随意的 try 异常。 我们需要处理的只有 非 runtimexception ,别的异常直接跑就是了。
推荐的异常处理方法:
1 定义 全局异常处理,然后根据 不同异常 ,返回不同的 提示信息。
全局异常处理类:
package com.hs.backend.controller.exception; import java.util.Map;
import java.util.Set; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import com.hs.commons.exception.CanRedirectExcetion;
import com.hs.commons.utils.ClassUtils; /**
* 统一异常处理
*
* @author ZHANGYUKUN
*
*/
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver { Logger logger = LoggerFactory.getLogger(GlobalExceptionResolver.class); @Autowired
RequestMappingHandlerMapping requestMappingHandlerMapping; /**
* 异常前缀
*/
private String prefix = "/global/exception/"; /**
* 异常后缀
*/
private String suffix = "Result"; /**
* 自定义异常列表
*
*/
//private List<Class<?>> exceptionClsList = new ArrayList<>(); /**
* 子包名
*/
//private String subPackage = "exception"; /**
* 默认的处理结果
*/
private String defaultResult = "unknownException"; @Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception exception) {
ModelAndView mv = new ModelAndView();
mv.setViewName(getForwardViewName(getViewName(defaultResult)));
mv.addObject("exception", exception);
Class<?> cls = exception.getClass();
String viewName = getViewName(ClassUtils.getObjectName(cls.getSimpleName()));
if (CanRedirectExcetion.class.isInstance(exception)) { Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
Set<RequestMappingInfo> set = map.keySet();
for (RequestMappingInfo requestMappingInfo : set) {
if (!requestMappingInfo.getPatternsCondition().getMatchingPatterns( viewName ).isEmpty()) {
mv.setViewName(getForwardViewName(getViewName(ClassUtils.getObjectName(cls.getSimpleName()))));
return mv;
}
} }
System.out.println( exception.toString() );
return mv;
} /**
* 通过 异常对象名 得到 异常视图名
* @param exceptionObjectName
* @return
*/
private String getViewName(String exceptionObjectName) {
return prefix + exceptionObjectName + suffix;
} /**
* 得到 forward 格式 的viewName
* @param viewName
* @return
*/
private String getForwardViewName(String viewName) {
return "forward:" + viewName;
} }
异常信息返回: 上面的 异常处理类会 调用下面这个类的不同方法 返回 前端 不同错误码 ,并且 统一格式的 信息。
package com.hs.backend.controller.exception; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import com.hs.backend.exception.GlobalExceptionResolver;
import com.hs.common.result.CommonErrorInfo;
import com.hs.common.result.CommonResult;
import com.hs.commons.exception.AlreadyExistRecord;
import com.hs.commons.exception.ApiInvokingException;
import com.hs.commons.exception.DataErrorException;
import com.hs.commons.exception.NoPermissionException;
import com.hs.commons.exception.NoTokenErrorException;
import com.hs.commons.exception.ParameterErrorException;
import com.hs.commons.exception.TokenExpiredException;
import com.hs.commons.exception.UnLoginException; import springfox.documentation.annotations.ApiIgnore; /**
* 定义异常的返回处理策略,这个路劲不应该添加热河权限限制
*
* @author ZHANGYUKUN
*/
@ApiIgnore
@RestController
@RequestMapping("global")
public class GlobalController { private static Logger logger = LoggerFactory.getLogger(GlobalExceptionResolver.class); /**
* 未知异统一处理
*
* @return
*/
@RequestMapping("exception/unknownExceptionResult")
public CommonResult<Void> unknownExceptionResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Exception exception = (Exception) request.getAttribute("exception"); Throwable throwable = null;
if (logger.isErrorEnabled()) {
logger.error("未知异常发生了:", exception);
throwable = findFinalCause( exception );;
} //如果取不到异常堆站里面的 信息 ,就向前端提示未知异常
String message = "未知的异常";
if( throwable.getMessage() != null ) {
message = throwable.getMessage();
}
return CommonResult.getFaiInstance(CommonErrorInfo.code_3001, message);
} /**
* 参错误统一返回
*
* @return
*/
@RequestMapping("exception/parameterErrorExceptionResult")
public CommonResult<Void> parameterErrorExceptionResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
ParameterErrorException exception = (ParameterErrorException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_1001, exception.getMessage() );
} /**
* 没有权限统一返回
* @return
*/
@RequestMapping("exception/noPermissionExceptionResult")
public CommonResult<Void> noPermissionException() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
NoPermissionException exception = (NoPermissionException)request.getAttribute("exception");
return CommonResult.getFaiInstance( CommonErrorInfo.code_2001 , exception.getMessage() == null ? "没有权限" : exception.getMessage());
} /**
* 未登录统一返回
*
* @return
*/
@RequestMapping("exception/unLoginExceptionResult")
public CommonResult<Void> unLoginExceptionResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
UnLoginException exception = (UnLoginException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_4001, exception.getMessage());
} /**
* 已存在的记录错误
* @return
*/
@RequestMapping("exception/alreadyExistRecordResult")
public CommonResult<Void> alreadyExistRecordResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
AlreadyExistRecord exception = (AlreadyExistRecord)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_5001, exception.getMessage() );
} /**
* 没有token
* @return
*/
@RequestMapping("exception/noTokenErrorExceptionResult")
public CommonResult<Void> noTokenErrorExceptionResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
NoTokenErrorException exception = (NoTokenErrorException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_2101, exception.getMessage() );
} /**
* token过期
* @return
*/
@RequestMapping("exception/tokenExpiredExceptionResult")
public CommonResult<Void> tokenExpiredExceptionResult() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
TokenExpiredException exception = (TokenExpiredException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_2102, exception.getMessage() );
} /**
* 数据错误
* @return
*/
@RequestMapping("exception/dataErrorExceptionResult")
public CommonResult<Void> dataErrorException() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
DataErrorException exception = (DataErrorException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_6001, exception.getMessage() );
} /**
* 访问频率过高
* @return
*/
@RequestMapping("exception/requestFrequencyExceptionResult")
public CommonResult<Void> requestFrequencyException() {
return CommonResult.getFaiInstance(CommonErrorInfo.code_8001, "访问频率过高" );
} /**
* api调用异常错误
* @return
*/
@RequestMapping("exception/apiInvokingExceptionResult")
public CommonResult<Void> apiInvokingException() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
ApiInvokingException exception = (ApiInvokingException)request.getAttribute("exception"); return CommonResult.getFaiInstance(CommonErrorInfo.code_9001, exception.getMessage() );
} /**
* 得到最根上的异常
*
* @param throwable
* @return
*/
private Throwable findFinalCause(Throwable throwable) {
Throwable rootThrowable = null;
String msg = null; if( throwable.getCause() == null ) {
rootThrowable = throwable;
}else {
rootThrowable = findFinalCause( throwable.getCause() );
}
msg = rootThrowable.getMessage();
System.out.println( msg ); return rootThrowable;
} }
正常的方法调用: 保持事物环境,不管是注解 还是 aop ,然后 不合理的 操作 ,直接 抛出异常 ,让整个事物环境回滚。(这里只有单个方法,如果 A 调用B ,B 里面抛出异常,B 的 事物传播 参数 指定的 是 REQUIRED(默认的),那么 就可以 正常回滚 )

Transaction rolled back because it has been marked as rollback-only 原因 和解决方案的更多相关文章
- “Transaction rolled back because it has been marked as rollback-only”
spring的声明事务提供了强大功能,让我们把业务关注和非业务关注的东西又分离开了.好东西的使用,总是需要有代价的.使用声明事务的时候,一 个不小心经常会碰到“Transaction rolled b ...
- Transaction rolled back because it has been marked as rollback-only
出现这种错误的原因 1.接口A 调用了接口B 2.接口B报异常了,没有在B里面进行try catch捕获 3.接口A对 接口B进行了try catch捕获 因为接口B报异常 会把当前事物A接口的事物( ...
- 【Spring】21、用spring目标对象处理Transaction rolled back because it has been marked as rollback-only
在使用spring做事务管理时,很多人都会遇到这样一段异常: org.springframework.transaction.UnexpectedRollbackException: Transact ...
- Transaction rolled back because it has been marked as rollback-only分析解决方法
1. Transaction rolled back because it has been marked as rollback-only事务已回滚,因为它被标记成了只回滚<prop key= ...
- 【springcloud】Transaction rolled back because it has been marked as rollback-only
问题: 一个ajax请求,发生系统错误,错误内容:Transaction rolled back because it has been marked as rollback-only 原因是调用的s ...
- [转]Spring事务嵌套引发的血案---Transaction rolled back because it has been marked as rollback-only
原文地址:https://blog.csdn.net/f641385712/article/details/80445912 1.概述 想必大家一想到事务,就想到ACID,或者也会想到CAP.但笔者今 ...
- Spring事务嵌套引发的问题--Transaction rolled back because it has been marked as rollback-only
转载https://blog.csdn.net/f641385712/article/details/80445912 读了两边才找到问题
- 记一次org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only异常
@Transactional(rollbackFor = Exception.class) @Overridepublic DubboResult<Boolean> productAddO ...
- Junit4测试报错
1.字符串数组越界 java.lang.String IndexOutOfBounds Exception:String index out of range:-1 导致: Transaction r ...
随机推荐
- Entity Framework框架 (二)
一.前面一篇EF的介绍主要是通过linq语句,本篇主要是介绍一下不通过linq语句如何去操作EF执行查询sql语句与执行存储过程. 1. 其中的Acccout是输出参数对应的类.比如输出参数有三个值 ...
- JS实时获取输入框中的值
实时获取input输入框中的值需要oninput和onpropertychange属性来实现.原因是onpropertychange属性为IE专属,而oninput属性支持大部分浏览器包括IE9及以上 ...
- Asp.net core 学习笔记 (AutoMapper)
参考 : http://www.cnblogs.com/xishuai/p/3700052.html http://www.cnblogs.com/xishuai/p/3704435.html htt ...
- Spring boot actuator端点启用和暴露
1.启用端点 默认情况下,除了shutdown端点是关闭的,其它的都是启用的.配置一个端点的启用,使用management.endpoint..enabled属性,下面的例子是启用shutdown端点 ...
- Hibernate向数据库存入BLOB和CLOB类型的数据
我选用的是byte[] +@Lob 刚开始采用的java.sql.Blob,将上传的图片getBytes()后,通过Hibernate.getLobCreator(HibernateSessionFa ...
- Android Studio 使用本地gradle配置详解
由于国内墙的原因,我们的Gradle无法使用 但是我们可以通过去下载我们想要的Gradle版本 然后再AndoidStudio内去配置本地的版本 进而去实现了Gradle的配置 注意一: so我们按照 ...
- python实现http get请求
接口请求方式为get请求,如下图抓包查看 Python实现脚本请求接口并以中文打印接口返回的数据 import urllib.parse import urllib.request url = &qu ...
- 『TensorFlow』读书笔记_Inception_V3_下
极为庞大的网络结构,不过下一节的ResNet也不小 线性的组成,结构大体如下: 常规卷积部分->Inception模块组1->Inception模块组2->Inception模块组3 ...
- mysql数据库的基础操作
注 : 本文中注释因为方便写了# ,mysql中规范应为 -- 创建用户 通过root管理员权限可以创建各种用户,并为他们分配权限及可以操作的数据库. 语法:create user '用户名'@'ip ...
- Oracle判断周末
有些业务场景下会有择出周末的需求,具体判断语句如下: 1.SELECT TO_CHAR(TO_DATE(DATA_DATE,'YYYY-MM-DD),'D') FROM DUAL; 如果DATA_DA ...