对了加了 @Transactional 的办法,当办法抛出异样时会主动回滚。如果咱们此时想在回滚前执行一些办法,如开释锁。能够这么写:
/**
* 实用于事务办法:事务实现后再开释锁
*
* @param key
* @param requestId
*/
private void unlockAfterTransaction(String key, String requestId) {TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCompletion(int status) {super.afterCompletion(status);
distributeLocker.unlock(key, requestId);
log.info("unlock success");
}
});
}