Urb*_*leg 8 java spring hibernate jpa optimistic-locking
我们决定在我们的Web应用程序中使用乐观锁定,以便增加并发性,而不使用悲观锁定.
我们现在正在寻找重试解决方案.
我们希望对我们当前的代码库产生尽可能小的影响.
我们在网上看到的解决方案之一是使用带注释的重试拦截器将方法标记为可重试.
问题是我们想要注释对它们有@Transactional注释的方法,但拦截器由于某种原因无法重试它们.(拦截器完美地重试非事务方法.)
所以:
1)是否有任何替代重试对我们的代码影响最小?
2)该解决方案是否有任何文档\教程?
3)是否可以重试@Transactional注释方法?
干杯!
当版本号或时间戳检查失败(发生乐观锁)时,您可以使用Spring Retry 重试事务方法。
@Configuration
@EnableRetry
public class RetryConfig {
}
Run Code Online (Sandbox Code Playgroud)
@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
Run Code Online (Sandbox Code Playgroud)
使用@Transactional (propagation = Propagation.REQUIRES_NEW)
重试仅从注释方法的代码。