Interface UnitOfWork
- All Known Subinterfaces:
ClientSessionAwareUnitOfWork,HandleAwareUnitOfWork
- All Known Implementing Classes:
GenericHandleAwareUnitOfWorkFactory.GenericHandleAwareUnitOfWork,SpringMongoTransactionAwareUnitOfWorkFactory.SpringMongoTransactionAwareUnitOfWork,SpringTransactionAwareJdbiUnitOfWorkFactory.SpringTransactionAwareHandleAwareUnitOfWork,SpringTransactionAwareUnitOfWork
public interface UnitOfWork
-
Method Summary
Modifier and TypeMethodDescriptionvoidcommit()Commit theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.CommittedThe cause of a Rollback or amarkAsRollbackOnly(Exception)default voidvoidmarkAsRollbackOnly(Exception cause) <T> TregisterLifecycleCallbackForResource(T resource, UnitOfWorkLifecycleCallback<T> associatedUnitOfWorkCallback) TODO: Adjust example to the new API Register a resource (e.g.default voidrollback()Roll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBackvoidRoll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBackvoidstart()Start theUnitOfWorkand any underlying transactionstatus()Get the status of theUnitOfWork
-
Method Details
-
start
void start()Start theUnitOfWorkand any underlying transaction -
commit
void commit()Commit theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.Committed -
rollback
Roll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBack- Parameters:
cause- the cause of the rollback
-
status
UnitOfWorkStatus status()Get the status of theUnitOfWork -
getCauseOfRollback
Exception getCauseOfRollback()The cause of a Rollback or amarkAsRollbackOnly(Exception) -
markAsRollbackOnly
default void markAsRollbackOnly() -
markAsRollbackOnly
-
rollback
default void rollback()Roll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBack -
registerLifecycleCallbackForResource
<T> T registerLifecycleCallbackForResource(T resource, UnitOfWorkLifecycleCallback<T> associatedUnitOfWorkCallback) TODO: Adjust example to the new API Register a resource (e.g. an Aggregate) that should have itsUnitOfWorkLifecycleCallbackcalled duringUnitOfWorkoperation.
Example:
Where the AggregAggregate aggregate = unitOfWork.registerLifecycleCallbackForResource(aggregate.loadFromEvents(event), new AggregateRootRepositoryUnitOfWorkLifecycleCallback()));class AggregateRootRepositoryUnitOfWorkLifecycleCallback implements UnitOfWorkLifecycleCallback<AGGREGATE_TYPE> { @Override public void beforeCommit(UnitOfWork unitOfWork, java.util.List<AGGREGATE_TYPE> associatedResources) { log.trace("beforeCommit processing {} '{}' registered with the UnitOfWork being committed", associatedResources.size(), aggregateType.getName()); associatedResources.forEach(aggregate -> { log.trace("beforeCommit processing '{}' with id '{}'", aggregateType.getName(), aggregate.aggregateId()); List<Object> persistableEvents = aggregate.uncommittedChanges(); if (persistableEvents.isEmpty()) { log.trace("No changes detected for '{}' with id '{}'", aggregateType.getName(), aggregate.aggregateId()); } else { if (log.isTraceEnabled()) { log.trace("Persisting {} event(s) related to '{}' with id '{}': {}", persistableEvents.size(), aggregateType.getName(), aggregate.aggregateId(), persistableEvents.map(persistableEvent -> persistableEvent.event().getClass().getName()).reduce((s, s2) -> s + ", " + s2)); } else { log.debug("Persisting {} event(s) related to '{}' with id '{}'", persistableEvents.size(), aggregateType.getName(), aggregate.aggregateId()); } eventStore.persist(unitOfWork, persistableEvents); aggregate.markChangesAsCommitted(); } }); } @Override public void afterCommit(UnitOfWork unitOfWork, java.util.List<AGGREGATE_TYPE> associatedResources) { } @Override public void beforeRollback(UnitOfWork unitOfWork, java.util.List<AGGREGATE_TYPE> associatedResources, Exception causeOfTheRollback) { } @Override public void afterRollback(UnitOfWork unitOfWork, java.util.List<AGGREGATE_TYPE> associatedResources, Exception causeOfTheRollback) { } }- Type Parameters:
T- the type of resource- Parameters:
resource- the resource that should be trackedassociatedUnitOfWorkCallback- the callback instance for the given resource- Returns:
- the
resourceor a proxy to it
-