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- thePlatformTransactionManagerspecialization that thisSpringTransactionAwareUnitOfWorkis compatible withUOW- theSpringTransactionAwareUnitOfWorkspecialization
- 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
Specializations can choose to override
UnitOfWorkSpecializations can choose to override
onStart() to initialize any datastore specific resources (e.g. a JDBI handle) and onCleanup() to release/cleanup the resources.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected SpringTransactionAwareUnitOfWorkFactory<TRX_MGR,UOW> protected Map<UnitOfWorkLifecycleCallback<Object>,List<Object>> -
Constructor Summary
ConstructorsConstructorDescriptionSpringTransactionAwareUnitOfWork(SpringTransactionAwareUnitOfWorkFactory<TRX_MGR, UOW> unitOfWorkFactory) SpringTransactionAwareUnitOfWork(SpringTransactionAwareUnitOfWorkFactory<TRX_MGR, UOW> unitOfWorkFactory, org.springframework.transaction.TransactionStatus manuallyManagedSpringTransaction) -
Method Summary
Modifier and TypeMethodDescriptionvoidcommit()Commit theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.CommittedThe cause of a Rollback or aUnitOfWork.markAsRollbackOnly(Exception)voidmarkAsRollbackOnly(Exception cause) protected voidCalled on clean up (e.g.protected voidonStart()Called when theUnitOfWorkis started.
Here any unit of work related resources (e.g.<T> TregisterLifecycleCallbackForResource(T resource, UnitOfWorkLifecycleCallback<T> associatedUnitOfWorkCallback) TODO: Adjust example to the new API Register a resource (e.g.voidRoll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBackvoidstart()Start theUnitOfWorkand any underlying transactionstatus()Get the status of theUnitOfWorkMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dk.cloudcreate.essentials.components.foundation.transaction.UnitOfWork
markAsRollbackOnly, rollback
-
Field Details
-
unitOfWorkFactory
protected SpringTransactionAwareUnitOfWorkFactory<TRX_MGR extends org.springframework.transaction.PlatformTransactionManager,UOW extends SpringTransactionAwareUnitOfWork<TRX_MGR, unitOfWorkFactoryUOW>> -
unitOfWorkLifecycleCallbackResources
protected Map<UnitOfWorkLifecycleCallback<Object>,List<Object>> unitOfWorkLifecycleCallbackResources
-
-
Constructor Details
-
SpringTransactionAwareUnitOfWork
public SpringTransactionAwareUnitOfWork(SpringTransactionAwareUnitOfWorkFactory<TRX_MGR, UOW> unitOfWorkFactory) -
SpringTransactionAwareUnitOfWork
public SpringTransactionAwareUnitOfWork(SpringTransactionAwareUnitOfWorkFactory<TRX_MGR, UOW> unitOfWorkFactory, org.springframework.transaction.TransactionStatus manuallyManagedSpringTransaction)
-
-
Method Details
-
start
public void start()Description copied from interface:UnitOfWorkStart theUnitOfWorkand any underlying transaction- Specified by:
startin interfaceUnitOfWork
-
onStart
protected void onStart()Called when theUnitOfWorkis 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 theUnitOfWork.
Here any unit of work related resources (e.g. JDBI handle) created inonStart()can be closed/cleaned-up -
commit
public void commit()Description copied from interface:UnitOfWorkCommit theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.Committed- Specified by:
commitin interfaceUnitOfWork
-
rollback
Description copied from interface:UnitOfWorkRoll back theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.RolledBack- Specified by:
rollbackin interfaceUnitOfWork- Parameters:
cause- the cause of the rollback
-
status
Description copied from interface:UnitOfWorkGet the status of theUnitOfWork- Specified by:
statusin interfaceUnitOfWork
-
getCauseOfRollback
Description copied from interface:UnitOfWorkThe cause of a Rollback or aUnitOfWork.markAsRollbackOnly(Exception)- Specified by:
getCauseOfRollbackin interfaceUnitOfWork
-
markAsRollbackOnly
- Specified by:
markAsRollbackOnlyin interfaceUnitOfWork
-
registerLifecycleCallbackForResource
public <T> T registerLifecycleCallbackForResource(T resource, UnitOfWorkLifecycleCallback<T> associatedUnitOfWorkCallback) Description copied from interface:UnitOfWorkTODO: 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) { } }- Specified by:
registerLifecycleCallbackForResourcein interfaceUnitOfWork- 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
-