/** * Invoked when an the given {@code aggregate} instance has been detected that has been part of a rolled back Unit * of Work. This typically means that the state of the Aggregate instance has been compromised and cannot be * guaranteed to be correct. * <p> * This implementation throws an exception, effectively causing the unit of work to be rolled back. Subclasses that * can guarantee correct storage, even when specific instances are compromised, may override this method to suppress * this exception. * <p> * When this method is invoked, the {@link #doSave(Aggregate)}, {@link #doDelete(Aggregate)}, * {@link #postSave(Aggregate)} and {@link #postDelete(Aggregate)} are not invoked. Implementations may choose to * invoke these methods. * * @param aggregate The aggregate instance with illegal state */ protected void reportIllegalState(A aggregate) { throw new AggregateRolledBackException(aggregate.identifierAsString()); }
@Override public A newInstance(Callable<T> factoryMethod) throws Exception { UnitOfWork<?> uow = CurrentUnitOfWork.get(); AtomicReference<A> aggregateReference = new AtomicReference<>(); // a constructor may apply events, and the persistence of an aggregate must take precedence over publishing its events. uow.onPrepareCommit(x -> prepareForCommit(aggregateReference.get())); A aggregate = doCreateNew(factoryMethod); aggregateReference.set(aggregate); Assert.isTrue(aggregateModel.entityClass().isAssignableFrom(aggregate.rootType()), () -> "Unsuitable aggregate for this repository: wrong type"); Map<String, A> aggregates = managedAggregates(uow); Assert.isTrue(aggregates.putIfAbsent(aggregate.identifierAsString(), aggregate) == null, () -> "The Unit of Work already has an Aggregate with the same identifier"); uow.onRollback(u -> aggregates.remove(aggregate.identifierAsString())); return aggregate; }
@Override protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion) { CurrentUnitOfWork.get().onRollback(u -> cache.remove(aggregate.identifierAsString())); super.validateOnLoad(aggregate, expectedVersion); }
/** * Checks the aggregate for concurrent changes. Throws a * {@link ConflictingModificationException} when conflicting changes have been * detected. * <p> * This implementation throws a {@link ConflictingAggregateVersionException} if the expected version is not null * and the version number of the aggregate does not match the expected version * * @param aggregate The loaded aggregate * @param expectedVersion The expected version of the aggregate * @throws ConflictingModificationException when conflicting changes have been detected * @throws ConflictingAggregateVersionException the expected version is not {@code null} * and the version number of the aggregate does not match the expected * version */ protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion) { if (expectedVersion != null && aggregate.version() != null && !expectedVersion.equals(aggregate.version())) { throw new ConflictingAggregateVersionException(aggregate.identifierAsString(), expectedVersion, aggregate.version()); } }
@Override protected LockAwareAggregate<T, A> doCreateNew(Callable<T> factoryMethod) throws Exception { A aggregate = doCreateNewForLock(factoryMethod); final String aggregateIdentifier = aggregate.identifierAsString(); Lock lock = lockFactory.obtainLock(aggregateIdentifier); try { CurrentUnitOfWork.get().onCleanup(u -> lock.release()); } catch (Throwable ex) { if (lock != null) { logger.debug("Exception occurred while trying to add an aggregate. Releasing lock.", ex); lock.release(); } throw ex; } return new LockAwareAggregate<>(aggregate, lock); }
@Override protected void validateOnLoad(Aggregate<T> aggregate, Long expectedVersion) { if (expectedVersion != null && expectedVersion < aggregate.version()) { DefaultConflictResolver conflictResolver = new DefaultConflictResolver(eventStore, aggregate.identifierAsString(), expectedVersion, aggregate.version()); ConflictResolution.initialize(conflictResolver); CurrentUnitOfWork.get().onPrepareCommit(uow -> conflictResolver.ensureConflictsResolved()); } else { super.validateOnLoad(aggregate, expectedVersion); } }