Spring Retry with Transactional

前端 未结 4 1601
余生分开走
余生分开走 2021-01-01 13:57

Is Spring Retry guaranteed to work with Spring\'s @Transactional annotation?

Specifically, I\'m trying to use @Retryable for optimistic loc

4条回答
  •  误落风尘
    2021-01-01 15:01

    If you want to test it independenty and be sure how it behaves then you may have @Transactional @Service, then another service that uses transaction one and just adds retries.

    In this case, no matter how much you test you are relying on undocumented behaviour (how exactly annotations processing is ordered). This may change between minor releases, based on order in which independent Spring beans are created, etc etc. In short, you are asking for problems when you mix @Transactional and @Retry on same method.

    edit: There is similar answered question https://stackoverflow.com/a/45514794/1849837 with code

    @Retryable(StaleStateException.class)
    @Transactional
    public void doSomethingWithFoo(Long fooId){
        // read your entity again before changes!
        Foo foo = fooRepository.findOne(fooId);
        foo.setStatus(REJECTED)  // <- sample foo modification
    } // commit on method end
    

    In that case it seems to be fine, because no matter what order is (retry then transaction, or transaction or retry) observable behaviour will be the same.

提交回复
热议问题