mapper-boot-starter icon indicating copy to clipboard operation
mapper-boot-starter copied to clipboard

使用mapper插件后,发现事务不回滚

Open lffsonic opened this issue 8 years ago • 2 comments

我使用springboot 1.5.6版本, 使用mybatis 官方starter进行设置,在service方法加上@Transactional(rollbackFor = Exception.class),方法默认最后都会出现异常,能对方法里面的更新数据库操作进行回滚,但是使用了pagehelp+mapper+druid starter插件后,发现更新的语句在数据库生效了,没有回滚。

lffsonic avatar Sep 04 '17 07:09 lffsonic

我在 https://github.com/abel533/MyBatis-Spring-Boot 这个项目做了个测试,没有发现问题。

在这个项目的 CountryService 中,增加了一个 1/0,如下:

@Transactional(rollbackFor = Exception.class)
public void save(Country country) {
    if (country.getId() != null) {
        countryMapper.updateByPrimaryKey(country);
    } else {
        countryMapper.insert(country);
    }
    System.out.println(1/0);
}

abel533 avatar Sep 04 '17 13:09 abel533

开启 trace 级别日志后:

2017-09-04 21:35:04.938 DEBUG 30456 --- [nio-9090-exec-1] t.m.s.m.C.updateByPrimaryKey             : ==>  Preparing: UPDATE country SET countryname = ?,countrycode = ? WHERE Id = ? 
2017-09-04 21:35:04.960 DEBUG 30456 --- [nio-9090-exec-1] t.m.s.m.C.updateByPrimaryKey             : ==> Parameters: ZZA(String), 333(String), 10009(Integer)
2017-09-04 21:35:04.964 DEBUG 30456 --- [nio-9090-exec-1] t.m.s.m.C.updateByPrimaryKey             : <==    Updates: 1
2017-09-04 21:35:04.964 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.mybatis.spring.SqlSessionHolder@545e5c13] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@78774a6c] bound to thread [http-nio-9090-exec-1]
2017-09-04 21:35:04.964 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.mybatis.spring.SqlSessionHolder@545e5c13] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@78774a6c] bound to thread [http-nio-9090-exec-1]
2017-09-04 21:35:04.964 DEBUG 30456 --- [nio-9090-exec-1] org.mybatis.spring.SqlSessionUtils       : Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11096a60]
2017-09-04 21:35:04.964 TRACE 30456 --- [nio-9090-exec-1] o.s.t.i.TransactionInterceptor           : Completing transaction for [tk.mybatis.springboot.service.CountryService.save] after exception: java.lang.ArithmeticException: / by zero
2017-09-04 21:35:04.964 TRACE 30456 --- [nio-9090-exec-1] o.s.t.i.RuleBasedTransactionAttribute    : Applying rules to determine whether transaction should rollback on java.lang.ArithmeticException: / by zero
2017-09-04 21:35:04.965 TRACE 30456 --- [nio-9090-exec-1] o.s.t.i.RuleBasedTransactionAttribute    : Winning rollback rule is: RollbackRuleAttribute with pattern [java.lang.Exception]
2017-09-04 21:35:04.965 TRACE 30456 --- [nio-9090-exec-1] o.s.j.d.DataSourceTransactionManager     : Triggering beforeCompletion synchronization
2017-09-04 21:35:04.965 DEBUG 30456 --- [nio-9090-exec-1] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization deregistering SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11096a60]
2017-09-04 21:35:04.965 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Removed value [org.mybatis.spring.SqlSessionHolder@545e5c13] for key [org.apache.ibatis.session.defaults.DefaultSqlSessionFactory@78774a6c] from thread [http-nio-9090-exec-1]
2017-09-04 21:35:04.965 DEBUG 30456 --- [nio-9090-exec-1] org.mybatis.spring.SqlSessionUtils       : Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11096a60]
2017-09-04 21:35:04.965 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@629ab059] for key [{
	CreateTime:"2017-09-04 21:34:06",
	ActiveCount:1,
	PoolingCount:0,
	CreateCount:1,
	DestroyCount:0,
	CloseCount:0,
	ConnectCount:1,
	Connections:[
	]
}] bound to thread [http-nio-9090-exec-1]
2017-09-04 21:35:04.965 DEBUG 30456 --- [nio-9090-exec-1] o.s.j.d.DataSourceTransactionManager     : Initiating transaction rollback
2017-09-04 21:35:04.965 DEBUG 30456 --- [nio-9090-exec-1] o.s.j.d.DataSourceTransactionManager     : Rolling back JDBC transaction on Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@468366]
2017-09-04 21:35:04.968 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Clearing transaction synchronization
2017-09-04 21:35:04.968 TRACE 30456 --- [nio-9090-exec-1] o.s.j.d.DataSourceTransactionManager     : Triggering afterCompletion synchronization
2017-09-04 21:35:04.968 TRACE 30456 --- [nio-9090-exec-1] .s.t.s.TransactionSynchronizationManager : Removed value [org.springframework.jdbc.datasource.ConnectionHolder@629ab059] for key [{
	CreateTime:"2017-09-04 21:34:06",
	ActiveCount:1,
	PoolingCount:0,
	CreateCount:1,
	DestroyCount:0,
	CloseCount:0,
	ConnectCount:1,
	Connections:[
	]
}] from thread [http-nio-9090-exec-1]
2017-09-04 21:35:04.969 DEBUG 30456 --- [nio-9090-exec-1] o.s.j.d.DataSourceTransactionManager     : Releasing JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@468366] after transaction
2017-09-04 21:35:04.969 DEBUG 30456 --- [nio-9090-exec-1] o.s.jdbc.datasource.DataSourceUtils      : Returning JDBC Connection to DataSource
2017-09-04 21:35:04.970 DEBUG 30456 --- [nio-9090-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public org.springframework.web.servlet.ModelAndView tk.mybatis.springboot.controller.CountryController.save(tk.mybatis.springboot.model.Country)]: java.lang.ArithmeticException: / by zero
2017-09-04 21:35:04.971 DEBUG 30456 --- [nio-9090-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [public org.springframework.web.servlet.ModelAndView tk.mybatis.springboot.controller.CountryController.save(tk.mybatis.springboot.model.Country)]: java.lang.ArithmeticException: / by zero
2017-09-04 21:35:04.971 DEBUG 30456 --- [nio-9090-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [public org.springframework.web.servlet.ModelAndView tk.mybatis.springboot.controller.CountryController.save(tk.mybatis.springboot.model.Country)]: java.lang.ArithmeticException: / by zero
2017-09-04 21:35:04.972 TRACE 30456 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@44e57b0d
2017-09-04 21:35:04.976 DEBUG 30456 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet        : Could not complete request

java.lang.ArithmeticException: / by zero
	at tk.mybatis.springboot.service.CountryService.save(CountryService.java:77) ~[classes/:na]
	at tk.mybatis.springboot.service.CountryService$$FastClassBySpringCGLIB$$afffef5.invoke(<generated>) ~[classes/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at tk.mybatis.springboot.service.CountryService$$EnhancerBySpringCGLIB$$2bf4bce2.save(<generated>) ~[classes/:na]
	at tk.mybatis.springboot.controller.CountryController.save(CountryController.java:89) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_51]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_51]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_51]
	at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_51]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]

abel533 avatar Sep 04 '17 13:09 abel533