How to roll Back OptimisticLock versionNumber

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);
}
}
}

Hi,

I think you’re right on this one. When an aggregate that passes the validation of the lock fails to persist, for some reason, the lock will reject any other aggregate. However, as soon as the last thread has “given up”, the lock will be released and ready for the next one.
I’ll have a look at how I can make sure the lock is released and made ready to accept the next aggregate when a rollback is performed.

Cheers,

Allard