异常信息:
2022-01-07 08:46:17.176$$[supplychain-thirdpard-service-www-7884fd7848-tg9z8]$$ERROR$$[supplychain-thirdpard-service-www]$$grpc-executor-[serverName]-6-313$$insertVoucherThirdByFlag$$com.hualala.supplychain.third.service.stock.ChainVoucherMiddleService$$fdd32c74-2db3-411d-9289-2446dc258a36$$92632$$doGenerateVoucher error2:java.lang.IllegalStateException: Transaction synchronization is not active
at org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(TransactionSynchronizationManager.java:291)
at com.hualala.supplychain.third.service.stock.insertvoucher.ChainFlag0Logic.doWithTransaction(ChainFlag0Logic.java:180)
at com.hualala.supplychain.third.service.stock.insertvoucher.ChainFlag0Logic.lambda$doGenerateVoucher$0(ChainFlag0Logic.java:113)
at com.hualala.supplychain.third.util.CacheUtils.lockResourceAndReleaseV4(CacheUtils.java:82)
at com.hualala.supplychain.third.service.stock.insertvoucher.ChainFlag0Logic.doGenerateVoucher(ChainFlag0Logic.java:111)
at com.hualala.supplychain.third.service.stock.insertvoucher.ChainFlag0Logic$$FastClassBySpringCGLIB$$4e370191.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
at com.hualala.supplychain.third.service.stock.insertvoucher.ChainFlag0Logic$$EnhancerBySpringCGLIB$$6dfadf23.doGenerateVoucher(<generated>)
at com.hualala.supplychain.third.service.stock.ChainVoucherMiddleService.insertVoucherThirdByFlag(ChainVoucherMiddleService.java:198)
原因:开启事务失败 方法上已经添加@Transactional注解开启事务,为什么事务开启失败? 总结了事务有效性相关的几点重要信息:
1.在需要事务管理的地方加@Transactional 注解。@Transactional 注解可以被应用于接口定义和接口方法、类定义和类的 public 方法上。
2.@Transactional 注解只能应用到 public 可见度的方法上。 如果你在 protected、private 或者 package-visible 的方法上使用 @Transactional 注解,它也不会报错, 但是这个被注解的方法将不会展示已配置的事务设置。
3.注意仅仅 @Transactional 注解的出现不足于开启事务行为,它仅仅 是一种元数据。必须在配置文件中使用配置元素,才真正开启了事务行为。(spring配置文件中,开启声明式事务)
4.通过 元素的 “proxy-target-class” 属性值来控制是基于接口的还是基于类的代理被创建。如果 “proxy-target-class” 属值被设置为 “true”,那么基于类的代理将起作用(这时需要CGLIB库cglib.jar在CLASSPATH中)。如果 “proxy-target-class” 属值被设置为 “false” 或者这个属性被省略,那么标准的JDK基于接口的代理将起作用。
5.Spring团队建议在具体的类(或类的方法)上使用 @Transactional 注解,而不要使用在类所要实现的任何接口上。在接口上使用 @Transactional 注解,只能当你设置了基于接口的代理时它才生效。因为注解是 不能继承 的,这就意味着如果正在使用基于类的代理时,那么事务的设置将不能被基于类的代理所识别,而且对象也将不会被事务代理所包装。
- @Transactional的事务开启 ,或者是基于接口的 或者是基于类的代理被创建。所以在同一个类中一个无事务的方法调用另一个有事务的方法,事务是不会起作用的。 重点是第6个! 同一个类中一个无事务的方法调用另一个有事务的方法,事务是不会起作用的。