NutzCN Logo
问答 spring boot + nutzboot事务不生效
发布于 1925天前 作者 qq_5c3d1d85 2043 次浏览 复制 上一个帖子 下一个帖子
标签:

环境:
spring boot :2.1.8.RELEASE
nutz boot : 1.r.66
数据库:Mysql
数据库连接池:druid
问题:spring 托管事务未生效


@SpringBootApplication @EnableTransactionManagement @ServletComponentScan @EnableScheduling public class MyApplication extends SpringBootServletInitializer{ @Override protected SpringApplicationBuilder configure(MyApplication builder) { return super.configure(builder); } public static void main(String[] args) { SpringApplication.run(SwiftApplication.class, args); } }
@Component
public class DaoConfig {
    @Autowired
    private Dao dao;

    @PostConstruct
    public void initNutzDaoWithSpringTransaction() {
        NutDao ss = (NutDao) this.dao;
        ss.setRunner(new SpringDaoRunner());
    }
}

 @Transactional
    public void updateAndSave(BizData bizData1, BizData bizData2) {
        dao.update(bizData1);  // 直接入库,未回滚

        int a = 0;
        int b = 1/a;   // 异常
       dao.insert(bizData2);
    }

异常日志

java.lang.ArithmeticException: / by zero
	at com.saicfc.swift.service.impl.FileServiceImpl.updateAndSave(FileServiceImpl.java:98)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205)
	at com.sun.proxy.$Proxy70.createLocalFile(Unknown Source)
	at com.saicfc.swift.service.impl.FileServiceImpl.createMsgFiles(FileServiceImpl.java:70)
	at com.saicfc.swift.cron.SendAndReceiveCron.createMsgFile(SendAndReceiveCron.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

断点第一行更新后,直接入库了,出错未回滚,请大神指点~~

9 回复

试了另外一种方式,将@EnableTransactionManagement、SpringDaoRunner、@Transactional全部删掉,仅适用Trans.exec,也不回滚

Trans.exec(new Atom() {
            @Override
            public void run() {
                updateAndSave(a,b);
            }
        });
java.lang.ArithmeticException: / by zero
	at com.saicfc.swift.service.impl.FileServiceImpl.updateAndSave(FileServiceImpl.java:103)
	at com.saicfc.swift.service.impl.FileServiceImpl$1.run(FileServiceImpl.java:87)
	at org.nutz.trans.Trans.exec(Trans.java:174)
	at org.nutz.trans.Trans.exec(Trans.java:132)
	at com.saicfc.swift.service.impl.FileServiceImpl.createLocalFile(FileServiceImpl.java:84)
	at com.saicfc.swift.service.impl.FileServiceImpl.createMsgFiles(FileServiceImpl.java:70)
	at com.saicfc.swift.cron.SendAndReceiveCron.createMsgFile(SendAndReceiveCron.java:65)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
	at java.util.concurrent.FutureTask.run(FutureTask.java)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

initNutzDaoWithSpringTransaction被执行没

@wendal 还请明示,initNutzDaoWithSpringTransaction怎么执行啊?

执行了,断点进去了

每执行一条sql操作,执行一次SpringDaoRunner->_run()

@wendal 与spring的scheduling有关系吗?

SpringRunner生效后,加Trans没作用的

FileServiceImpl不是spring bean?

看报错信息,这个类就没有被spring aop接管,也就不会有aop事务

添加回复
请先登陆
回到顶部