Spring nested transactions

前端 未结 5 1302
野趣味
野趣味 2021-02-01 03:05

In my Spring Boot project I have implemented following service method:

@Transactional
public boolean validateBoard(Board board) {
    boolean result = false;
            


        
5条回答
  •  面向向阳花
    2021-02-01 03:45

    Your problem is a method's call from another method inside the same proxy.It's self-invocation. In your case, you can easily fix it without moving a method inside another service (why do you need to create another service just for moving some method from one service to another just for avoid self-invocation?), just to call the second method not directly from current class, but from spring container. In this case you call proxy second method with transaction not with self-invocatio.

    This principle is useful for any proxy-object when you need self-invocation, not only a transactional proxy.

    @Service
    class SomeService ..... {
        -->> @Autorired
        -->> private ApplicationContext context;
        -->> //or with implementing ApplicationContextAware
    
        @Transactional(any propagation , it's not important in this case)
        public boolean methodOne(SomeObject object) {
          .......
           -->> here you get a proxy from context and call a method from this proxy
           -->>context.getBean(SomeService.class).
                methodTwo(object);
          ......
       }
    
        @Transactional(any propagation , it's not important in this case)public boolean 
        methodTwo(SomeObject object) {
        .......
       }
    }
    

    when you do call context.getBean(SomeService.class).methodTwo(object); container returns proxy object and on this proxy you can call methodTwo(...) with transaction.

提交回复
热议问题