Java springboot中的同步事务
在Springboot中,我调用的每个服务都会打开一个事务,当服务返回时,它会关闭该连接,但在我的情况下,我需要创建一个运行同步的方法(该方法只在非同步的方法中运行),如果有一个事务打开或没有打开,他需要独立地打开和关闭事务,该方法中的每个SQL操作只有在该方法抛出错误时才会回滚。如果调用它的方法抛出错误,他将不会回滚同步方法所做的任何操作 因此,我尝试使用以下示例:Java springboot中的同步事务,java,spring-boot,spring-data-jpa,Java,Spring Boot,Spring Data Jpa,在Springboot中,我调用的每个服务都会打开一个事务,当服务返回时,它会关闭该连接,但在我的情况下,我需要创建一个运行同步的方法(该方法只在非同步的方法中运行),如果有一个事务打开或没有打开,他需要独立地打开和关闭事务,该方法中的每个SQL操作只有在该方法抛出错误时才会回滚。如果调用它的方法抛出错误,他将不会回滚同步方法所做的任何操作 因此,我尝试使用以下示例: @Service public class MyService { @Autowired private MyR
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
public void methodNotSyncronized(String arg1, String arg2){
logger.debug("init method no syncronied");
MyObjct myObj = myRepository.findOne(1);
methodSyncronized(arg2);
myRepository.save(myObj); //If I got some error here everything that methodSyncronized did should remaining
logger.debug("finish method no syncronied");
}
@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.REQUIRES_NEW)
private synchronized String methodSyncronized(String arg){
logger.debug("init method syncronied");
//Here I will insert or delete something
}
}
但当我调试这段代码时,我得到了:
o.h.e.t.internal.TransactionImpl : begin
myService : init method no syncronied
myService : init method syncronied
myService : finish method no syncronied
o.h.e.t.internal.TransactionImpl : committing
我怎样才能解决这个问题
还有一件事,我调用的每个服务,即使我只对hibernate打印的数字求和:
o.h.e.t.internal.TransactionImpl : begin
o.h.e.t.internal.TransactionImpl : committing
即使我将@Transactional(readOnly=true)放在方法中它也不起作用,因为同一类中的方法调用的
Spring@Transaction方法不起作用
:这是Spring AOP(动态对象和cglib)的一个限制
勾选此项:您强调的
同步
最初让我对您试图实现的目标感到困惑-但似乎根本问题在于传播。REQUIRES\u NEW
不是创建/提交自己的事务,而是重用现有事务?如何在应用程序中配置事务管理?