Class SpringTransactionAwareUnitOfWork<TRX_MGR extends org.springframework.transaction.PlatformTransactionManager,UOW extends SpringTransactionAwareUnitOfWork<TRX_MGR,UOW>>

java.lang.Object
dk.cloudcreate.essentials.components.foundation.transaction.spring.SpringTransactionAwareUnitOfWork<TRX_MGR,UOW>
Type Parameters:
TRX_MGR - the PlatformTransactionManager specialization that this SpringTransactionAwareUnitOfWork is compatible with
UOW - the SpringTransactionAwareUnitOfWork specialization
All Implemented Interfaces:
UnitOfWork
Direct Known Subclasses:
SpringMongoTransactionAwareUnitOfWorkFactory.SpringMongoTransactionAwareUnitOfWork, SpringTransactionAwareJdbiUnitOfWorkFactory.SpringTransactionAwareHandleAwareUnitOfWork

public class SpringTransactionAwareUnitOfWork<TRX_MGR extends org.springframework.transaction.PlatformTransactionManager,UOW extends SpringTransactionAwareUnitOfWork<TRX_MGR,UOW>> extends Object implements UnitOfWork
Spring transaction-aware UnitOfWork
Specializations can choose to override onStart() to initialize any datastore specific resources (e.g. a JDBI handle) and onCleanup() to release/cleanup the resources.
  • Field Details

  • 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
    • onStart

      protected void onStart()
      Called when the UnitOfWork is started.
      Here any unit of work related resources (e.g. JDBI handle) can be created
    • onCleanup

      protected void onCleanup()
      Called on clean up (e.g. after commit/rollback) of the UnitOfWork.
      Here any unit of work related resources (e.g. JDBI handle) created in onStart() can be closed/cleaned-up
    • 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
    • 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
    • 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