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(Class<T> aggregateType)
          Initialize a repository with a pessimistic locking strategy.
protected LockingRepository(Class<T> aggregateType, LockManager lockManager)
          Initialize the repository with the given lockManager.
 
Method Summary
 void add(T aggregate)
          Adds the given aggregate to the repository.
protected  void doDelete(T aggregate)
          Verifies whether all locks are valid and delegates to doDeleteWithLock(org.axonframework.domain.AggregateRoot) to perform actual deleting.
protected abstract  void doDeleteWithLock(T aggregate)
          Perform the actual deleting of the aggregate.
protected abstract  T doLoad(Object 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(Object 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
getAggregateType, load, postDelete, postSave, 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(Class<T> aggregateType)
Initialize a repository with a pessimistic locking strategy.

Parameters:
aggregateType - The type of aggregate stored in this repository

LockingRepository

protected LockingRepository(Class<T> aggregateType,
                            LockManager lockManager)
Initialize the repository with the given lockManager.

Parameters:
aggregateType - The type of aggregate stored in this repository
lockManager - the lock manager to use
Method Detail

add

public void add(T aggregate)
Description copied from class: AbstractRepository
Adds the given aggregate to the repository. The version of this aggregate must be null, indicating that it has not been previously persisted.

This method will not force the repository to save the aggregate immediately. Instead, it is registered with the current UnitOfWork. To force storage of an aggregate, commit the current unit of work (CurrentUnitOfWork.commit())

Specified by:
add in interface Repository<T extends AggregateRoot>
Overrides:
add in class AbstractRepository<T extends AggregateRoot>
Parameters:
aggregate - The aggregate to add to the repository.

load

public T load(Object 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

doDelete

protected final void doDelete(T aggregate)
Verifies whether all locks are valid and delegates to doDeleteWithLock(org.axonframework.domain.AggregateRoot) to perform actual deleting.

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

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

doDeleteWithLock

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

Parameters:
aggregate - the aggregate to delete

doLoad

protected abstract T doLoad(Object 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 © 2010-2016. All Rights Reserved.