我使用Spring 2.5和Hibernate JPA实现Java和"容器"管理事务.
我有一个"用户提交后"方法,它在后台更新数据,无论是异常ConcurrencyFailureException还是StaleObjectStateException异常都需要提交,因为它永远不会显示给客户端.换句话说,需要将乐观锁定变为悲观.(如果方法执行需要更长的时间并且有人在其他事务中更改了数据,则可能发生)
我读了很多关于幂等的东西,如果异常搜索DEFAULT_MAX_RETRIES或6.2.7则重试.示例或第14.5章.重试.我也在这里和这里找到了stackoverflow .
我试过这个:
public aspect RetryOnConcurrencyExceptionAspect {
private static final int DEFAULT_MAX_RETRIES = 20;
private int maxRetries = DEFAULT_MAX_RETRIES;
Object around(): execution( * * (..) ) && @annotation(RetryOnConcurrencyException) && @annotation(Transactional) {
int numAttempts = 0;
RuntimeException failureException = null;
do {
numAttempts++;
try {
return proceed();
}
catch( OptimisticLockingFailureException ex ) {
failureException = ex;
}
catch(ConcurrencyFailureException ex) { …Run Code Online (Sandbox Code Playgroud)