Class GenericHandleAwareUnitOfWorkFactory.GenericHandleAwareUnitOfWork

java.lang.Object
dk.cloudcreate.essentials.components.foundation.transaction.jdbi.GenericHandleAwareUnitOfWorkFactory.GenericHandleAwareUnitOfWork
All Implemented Interfaces:
HandleAwareUnitOfWork, UnitOfWork
Enclosing class:
GenericHandleAwareUnitOfWorkFactory<UOW extends HandleAwareUnitOfWork>

public static class GenericHandleAwareUnitOfWorkFactory.GenericHandleAwareUnitOfWork extends Object implements HandleAwareUnitOfWork
  • Constructor Details

  • Method Details

    • start

      public void start()
      Description copied from interface: UnitOfWork
      Start the UnitOfWork and any underlying transaction
      Specified by:
      start in interface UnitOfWork
    • commit

      public void commit()
      Description copied from interface: UnitOfWork
      Commit the UnitOfWork and any underlying transaction - see UnitOfWorkStatus.Committed
      Specified by:
      commit in interface UnitOfWork
    • rollback

      public void rollback(Exception cause)
      Description copied from interface: UnitOfWork
      Roll back the UnitOfWork and any underlying transaction - see UnitOfWorkStatus.RolledBack
      Specified by:
      rollback in interface UnitOfWork
      Parameters:
      cause - the cause of the rollback
    • registerLifecycleCallbackForResource

      public <T> T registerLifecycleCallbackForResource(T resource, UnitOfWorkLifecycleCallback<T> associatedUnitOfWorkCallback)
      Description copied from interface: UnitOfWork
      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) {
      
           }
       }
       
       
      Specified by:
      registerLifecycleCallbackForResource in interface UnitOfWork
      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
    • status

      public UnitOfWorkStatus status()
      Description copied from interface: UnitOfWork
      Get the status of the UnitOfWork
      Specified by:
      status in interface UnitOfWork
    • getCauseOfRollback

      public Exception getCauseOfRollback()
      Description copied from interface: UnitOfWork
      The cause of a Rollback or a UnitOfWork.markAsRollbackOnly(Exception)
      Specified by:
      getCauseOfRollback in interface UnitOfWork
    • markAsRollbackOnly

      public void markAsRollbackOnly(Exception cause)
      Specified by:
      markAsRollbackOnly in interface UnitOfWork
    • handle

      public org.jdbi.v3.core.Handle handle()
      Description copied from interface: HandleAwareUnitOfWork
      Get the Jdbi handle associated with the current UnitOfWork
      Specified by:
      handle in interface HandleAwareUnitOfWork
      Returns:
      the Jdbi handle associated with the current UnitOfWork
    • afterClosingHandle

      protected void afterClosingHandle()
      Called right after the Handle is closed. Perform any additional clean-ups by overriding this method
    • afterCommitting

      protected void afterCommitting()
      Called right after Handle.commit() has been called.
    • beforeCommitting

      protected void beforeCommitting()
      Called right before Handle.commit() is called.
    • afterRollback

      protected void afterRollback(Exception cause)
      Called right after Handle.rollback() has been called.
      Parameters:
      cause - the cause of the rollback
    • beforeRollback

      protected void beforeRollback(Exception cause)
      Called right before Handle.rollback() is called.
      Parameters:
      cause - the cause of the rollback