org.axonframework.repository
Class LockingRepository<T extends AggregateRoot>

java.lang.Object
  extended by org.axonframework.repository.AbstractRepository<T>
      extended by org.axonframework.repository.LockingRepository<T>
Type Parameters:
T - The type that this aggregate stores
All Implemented Interfaces:
Repository<T>
Direct Known Subclasses:
EventSourcingRepository, GenericJpaRepository

public abstract class LockingRepository<T extends AggregateRoot>
extends AbstractRepository<T>

Implementation of the Repository interface that takes provides a locking mechanism to prevent concurrent modifications of persisted aggregates. Unless there is a locking mechanism present in the underlying persistence environment, it is recommended to use a LockingRepository (or one of its subclasses).

The LockingRepository can be initialized with two strategies:

Important: If an exception is thrown during the saving process, any locks held are released. The calling thread may reattempt saving the aggregate again. If the lock is available, the thread automatically takes back the lock. If, however, another thread has obtained the lock first, a ConcurrencyException is thrown.

Since:
0.3
Author:
Allard Buijze

Constructor Summary
protected LockingRepository()
          Initialize a repository with an optimistic locking strategy.
protected LockingRepository(LockingStrategy lockingStrategy)
          Initialize the repository with the given lockingStrategy.
 
Method Summary
protected abstract  T doLoad(AggregateIdentifier aggregateIdentifier, Long expectedVersion)
          Perform the actual loading of an aggregate.
protected  void doSave(T aggregate)
          Verifies whether all locks are valid and delegates to doSaveWithLock(org.axonframework.domain.AggregateRoot) to perform actual storage.
protected abstract  void doSaveWithLock(T aggregate)
          Perform the actual saving of the aggregate.
 T load(AggregateIdentifier aggregateIdentifier, Long expectedVersion)
          Load the aggregate with the given unique aggregateIdentifier, expecting the version of the aggregate to be equal to the given expectedVersion.
 
Methods inherited from class org.axonframework.repository.AbstractRepository
add, load, setEventBus, validateOnLoad
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LockingRepository

protected LockingRepository()
Initialize a repository with an optimistic locking strategy.


LockingRepository

protected LockingRepository(LockingStrategy lockingStrategy)
Initialize the repository with the given lockingStrategy.

Parameters:
lockingStrategy - the locking strategy to use
Method Detail

load

public T load(AggregateIdentifier aggregateIdentifier,
              Long expectedVersion)
Load the aggregate with the given unique aggregateIdentifier, expecting the version of the aggregate to be equal to the given expectedVersion. If the expectedVersion is null, no version validation is done.

When versions do not match, implementations may either raise an exception immediately when loading an aggregate, or at any other time while the aggregate is registered in the current Unit Of Work.

Specified by:
load in interface Repository<T extends AggregateRoot>
Overrides:
load in class AbstractRepository<T extends AggregateRoot>
Parameters:
aggregateIdentifier - The identifier of the aggregate to load
expectedVersion - The expected version of the aggregate to load, or null to indicate the version should not be checked
Returns:
The aggregate root with the given identifier.
Throws:
AggregateNotFoundException - if aggregate with given id cannot be found
RuntimeException - any exception thrown by implementing classes
See Also:
UnitOfWork

doSave

protected final void doSave(T aggregate)
Verifies whether all locks are valid and delegates to doSaveWithLock(org.axonframework.domain.AggregateRoot) to perform actual storage.

Specified by:
doSave in class AbstractRepository<T extends AggregateRoot>
Parameters:
aggregate - the aggregate to store

doSaveWithLock

protected abstract void doSaveWithLock(T aggregate)
Perform the actual saving of the aggregate. All necessary locks have been verified.

Parameters:
aggregate - the aggregate to store

doLoad

protected abstract T doLoad(AggregateIdentifier aggregateIdentifier,
                            Long expectedVersion)
Perform the actual loading of an aggregate. The necessary locks have been obtained.

Specified by:
doLoad in class AbstractRepository<T extends AggregateRoot>
Parameters:
aggregateIdentifier - the identifier of the aggregate to load
expectedVersion - The expected version of the aggregate
Returns:
the fully initialized aggregate
Throws:
AggregateNotFoundException - if aggregate with given id cannot be found


Copyright © 2011. All Rights Reserved.