Retry mechanism for optimistic locking (spring data + JPA)

核能气质少年 提交于 2019-12-01 03:49:22

Ad 3.

You can use Spring Retry to retry transacted method when a version number or timestamp check failed (optimistic lock occurs).

Configuration

@Configuration
@EnableRetry
public class RetryConfig {

}

Usage

@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

Use @Transational(propagation = REQUIRED_NEW) for retrying only the code from annotated method.

Ashish Jagtap
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!