NutzCN Logo
精华 在action的函数里,将同一个对象插入两次,事务没启到作用?
发布于 3256天前 作者 qq_5208486d 2047 次浏览 复制 上一个帖子 下一个帖子
标签:

本来插入两次,应该是 出现 “唯一主键约束”的异常,然后,应该事物回滚,但是插入一条成功了,代码如下:

    @At
    @Aop(TransAop.READ_COMMITTED)
    public void insert(@Param("i")String i)throws Exception {
        int aa=Integer.parseInt(i);
        User u = new User(aa, "111", new Date());
        dao.insert(u);
        User u2 = new User(aa, "222", new Date());
        try {
            dao.insert(u2);
        } catch (Exception e) {
            e.printStackTrace();
            throw  e;
        } finally {

        }
    }

6 回复

把打印的日志也贴一下

然后,也有人提出,这样写,但不够干净利落

try {
    Trans.begin();
   // ...............
} catch (...) {
   Trans.rollback();
} finally {
  Trans.close();
}

不需要这种手动方式

@wendal
异常如下:

java.sql.SQLIntegrityConstraintViolationException: ORA-00001: 违反唯一约束条件 (SCOTT.PK_T_USER)

	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:513) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:227) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:208) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1046) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714) ~[ojdbc6.jar:11.2.0.4.0]
	at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378) ~[ojdbc6.jar:11.2.0.4.0]
	at net.sf.log4jdbc.PreparedStatementSpy.execute(PreparedStatementSpy.java:417) ~[log4jdbc-1.2.jar:na]
	at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:67) ~[c3p0-0.9.5.1.jar:0.9.5.1]
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:266) ~[nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:81) ~[nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:279) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:36) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:202) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP._aop_invoke(NutDao.java:70) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.LoggerAop.filter(LoggerAop.java:28) [LoggerAop.class:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP.run(NutDao.java:1) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:269) [nutz-1.r.54.r2.jar:na]
	at org.nutz.trans.Trans.exec(Trans.java:174) [nutz-1.r.54.r2.jar:na]
	at org.nutz.trans.Trans.exec(Trans.java:132) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:239) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP._aop_invoke(NutDao.java:78) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.LoggerAop.filter(LoggerAop.java:28) [LoggerAop.class:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP._exec(NutDao.java:1) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao.insert(NutDao.java:144) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP._aop_invoke(NutDao.java:18) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.LoggerAop.filter(LoggerAop.java:28) [LoggerAop.class:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) [nutz-1.r.54.r2.jar:na]
	at org.nutz.dao.impl.NutDao$$NUTZAOP.insert(NutDao.java:1) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.logg.insert(logg.java:102) [logg.class:na]
	at com.kanq.core.context.logg$$NUTZAOP._aop_invoke(logg.java:1) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34) [nutz-1.r.54.r2.jar:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.LoggerAop.filter(LoggerAop.java:28) [LoggerAop.class:na]
	at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60) [nutz-1.r.54.r2.jar:na]
	at com.kanq.core.context.logg$$NUTZAOP.insert(logg.java:1) [nutz-1.r.54.r2.jar:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.6.0_20]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) ~[na:1.6.0_20]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ~[na:1.6.0_20]
	at java.lang.reflect.Method.invoke(Method.java:597) ~[na:1.6.0_20]
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:119) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31) [nutz-1.r.54.r2.jar:na]
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:179) [nutz-1.r.54.r2.jar:na]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.42]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.42]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [catalina.jar:7.0.42]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [catalina.jar:7.0.42]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:7.0.42]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) [catalina.jar:7.0.42]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [catalina.jar:7.0.42]
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) [catalina.jar:7.0.42]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.42]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) [catalina.jar:7.0.42]
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) [tomcat-coyote.jar:7.0.42]
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) [tomcat-coyote.jar:7.0.42]
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852) [tomcat-coyote.jar:7.0.42]
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_20]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_20]
	at java.lang.Thread.run(Thread.java:619) [na:1.6.0_20]

TransactionInterceptor 给debug一下看看发生了什么事

@wendal
谢谢了
在切面日志这里没抛出去,抛出去就好了

 @Override
    public void filter(InterceptorChain chain) throws Throwable {
        Stopwatch sw = Stopwatch.begin();//开始计时
        Throwable throwable = null;
        try {
            chain.doChain();//执行方法
        } catch (Throwable t) {
            throwable = t;
            throw t;
        } finally {
添加回复
请先登陆
回到顶部