All Known Subinterfaces:
ClientSessionAwareUnitOfWork, HandleAwareUnitOfWork
All Known Implementing Classes:
GenericHandleAwareUnitOfWorkFactory.GenericHandleAwareUnitOfWork, SpringMongoTransactionAwareUnitOfWorkFactory.SpringMongoTransactionAwareUnitOfWork, SpringTransactionAwareJdbiUnitOfWorkFactory.SpringTransactionAwareHandleAwareUnitOfWork, SpringTransactionAwareUnitOfWork

public interface UnitOfWork
  • Method Details

    • start

      void start()
      Start the UnitOfWork and any underlying transaction
    • commit

      void commit()
      Commit the UnitOfWork and any underlying transaction - see UnitOfWorkStatus.Committed
    • rollback

      void rollback(Exception cause)
      Roll back the UnitOfWork and any underlying transaction - see UnitOfWorkStatus.RolledBack
      Parameters:
      cause - the cause of the rollback
    • status

      Get the status of the UnitOfWork
    • getCauseOfRollback

      Exception getCauseOfRollback()
      The cause of a Rollback or a markAsRollbackOnly(Exception)
    • markAsRollbackOnly

      default void markAsRollbackOnly()
    • markAsRollbackOnly

      void markAsRollbackOnly(Exception cause)
    • rollback

      default void rollback()
      Roll back the UnitOfWork and any underlying transaction - see UnitOfWorkStatus.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 its UnitOfWorkLifecycleCallback called during UnitOfWork operation.
      Example:
      
       Aggregate aggregate = unitOfWork.registerLifecycleCallbackForResource(aggregate.loadFromEvents(event),
                                                                             new AggregateRootRepositoryUnitOfWorkLifecycleCallback()));
       
      Where the Aggreg
      
       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 tracked
      associatedUnitOfWorkCallback - the callback instance for the given resource
      Returns:
      the resource or a proxy to it