Hi,
i am processing concurrent problem of Repository, and i want to use OptimisticLockManager, but the inner class OptimisticLock only have init and increase method for versionNumber.
When i want to rollback after increase method validate(), how to do this. On the current situation, If one Thread commit transaction failed, the versionNumber won’t roll back , any other
Thread will failed too. How to solve this problem? There is OptmisticLock class source code:
public class OptimisticLockManager implements LockManager {
private final ConcurrentHashMap<Object, OptimisticLock> locks = new ConcurrentHashMap<Object, OptimisticLock>();
…
private final class OptimisticLock {
private Long versionNumber;
private final Map<Thread, Integer> threadsHoldingLock = new WeakHashMap<Thread, Integer>();
private boolean closed = false;
private OptimisticLock() {
}
private synchronized boolean validate(AggregateRoot aggregate) {
Long lastCommittedEventSequenceNumber = aggregate.getVersion();
if (versionNumber == null || versionNumber.equals(lastCommittedEventSequenceNumber)) {
long last = lastCommittedEventSequenceNumber == null ? 0 : lastCommittedEventSequenceNumber;
versionNumber = last + aggregate.getUncommittedEventCount();
return true;
}
return false;
}
private synchronized boolean lock() {
if (closed) {
return false;
}
Integer lockCount = threadsHoldingLock.get(Thread.currentThread());
if (lockCount == null) {
lockCount = 0;
}
threadsHoldingLock.put(Thread.currentThread(), lockCount + 1);
return true;
}
private synchronized void unlock(Object aggregateIdentifier) {
Integer lockCount = threadsHoldingLock.get(Thread.currentThread());
if (lockCount == null || lockCount == 1) {
threadsHoldingLock.remove(Thread.currentThread());
} else {
threadsHoldingLock.put(Thread.currentThread(), lockCount - 1);
}
if (threadsHoldingLock.isEmpty()) {
closed = true;
locks.remove(aggregateIdentifier, this);
}
}
}