/** * Verifies whether all locks are valid and delegates to * {@link #doDeleteWithLock(Aggregate)} to perform actual deleting. * * @param aggregate the aggregate to delete */ @Override protected final void doDelete(LockAwareAggregate<T, A> aggregate) { if (aggregate.version() != null && !aggregate.isLockHeld()) { throw new ConcurrencyException(String.format( "The aggregate of type [%s] with identifier [%s] could not be " + "saved, as a valid lock is not held. Either another thread has saved an aggregate, or " + "the current thread had released its lock earlier on.", aggregate.getClass().getSimpleName(), aggregate.identifierAsString())); } doDeleteWithLock(aggregate.getWrappedAggregate()); }
/** * Verifies whether all locks are valid and delegates to * {@link #doSaveWithLock(Aggregate)} to perform actual storage. * * @param aggregate the aggregate to store */ @Override protected void doSave(LockAwareAggregate<T, A> aggregate) { if (aggregate.version() != null && !aggregate.isLockHeld()) { throw new ConcurrencyException(String.format( "The aggregate of type [%s] with identifier [%s] could not be " + "saved, as a valid lock is not held. Either another thread has saved an aggregate, or " + "the current thread had released its lock earlier on.", aggregate.getClass().getSimpleName(), aggregate.identifierAsString())); } doSaveWithLock(aggregate.getWrappedAggregate()); }
/** * Invoke when an Exception is raised while persisting an Event or Snapshot. * * @param exception The exception raised while persisting an Event * @param failedEvent The EventMessage that could not be persisted */ protected void handlePersistenceException(Exception exception, EventMessage<?> failedEvent) { String eventDescription; if (failedEvent instanceof DomainEventMessage<?>) { DomainEventMessage<?> failedDomainEvent = (DomainEventMessage<?>) failedEvent; eventDescription = format("An event for aggregate [%s] at sequence [%d]", failedDomainEvent.getAggregateIdentifier(), failedDomainEvent.getSequenceNumber()); } else { eventDescription = format("An event with identifier [%s]", failedEvent.getIdentifier()); } if (persistenceExceptionResolver != null && persistenceExceptionResolver.isDuplicateKeyViolation(exception)) { throw new ConcurrencyException(eventDescription + " was already inserted", exception); } else { throw new EventStoreException(eventDescription + " could not be persisted", exception); } }