JFinal声明了事务控制的方法抛出异常后数据没有回滚

pandyyan 发布于 2013/05/02 10:17
阅读 2K+
收藏 0

@JFinal 你好,想跟你请教个问题:

我有这样一个测试方法,添加两条数据到mysql数据库,第二条与第一条数据完全一样,再save时会报“Duplicate entry '69d807a9-a266-60e7-b403-ff3005b7d07c' for key 1”异常,按理说第一条save的数据应该被回滚掉才对,但查询数据库发现该条数据还在,请问这个问题怎么解决?谢谢~~

2013-05-02 09:42:08
[ERROR]-[Thread: qtp9394536-23]-[com.test.controller.sys.RoleController.addwithoptAuth()]: com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '69d807a9-a266-60e7-b403-ff3005b7d07c' for key 1
	at com.jfinal.plugin.activerecord.Model.save(Model.java:298)
	at com.test.controller.sys.RoleController.addwithoptAuth(RoleController.java:110)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:601)
	at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:55)
	at com.jfinal.plugin.activerecord.tx.Tx.intercept(Tx.java:47)
	at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:51)
	at com.test.interceptor.coms.ComsLogInterceptor.intercept(ComsLogInterceptor.java:27)
	at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:51)
	at com.test.interceptor.sys.SessionInterceptor.intercept(SessionInterceptor.java:20)
	at com.jfinal.core.ActionInvocation.invoke(ActionInvocation.java:51)
	at com.jfinal.core.ActionHandler.handle(ActionHandler.java:75)
	at com.jfinal.plugin.druid.DruidStatViewHandler.handle(DruidStatViewHandler.java:58)
	at com.jfinal.ext.handler.ContextPathHandler.handle(ContextPathHandler.java:47)
	at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
	at org.eclipse.jetty.server.Server.handle(Server.java:365)
	at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
	at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:937)
	at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:998)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
	at java.lang.Thread.run(Thread.java:722)

 

加载中
0
pandyyan
pandyyan
我知道问题的原因了,我在事务方法中对CRUD操作加了try..catch捕获异常后,没有再次throw将异常抛出,导致Tx中的try..catch不能捕获到异常,导致conn.rollback没有被调用,所以回滚失败。
0
沉寂brain
沉寂brain
看看是否数据库开启了事务  是否是I nnoDB
pandyyan
pandyyan
事务已开启,确实为InnoDB引擎,问题我已经找到,谢谢!
0
pandyyan
pandyyan
@JFinal 如果问题确实如我定位的一样,JFinal的事务控制是不是在使用try..catch处理异常后,一定要throw new ActiveRecordException(e) 异常呢?有没有更好的解决办法呢?
返回顶部
顶部