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 Summary
ConstructorsConstructorDescriptionGenericHandleAwareUnitOfWork(GenericHandleAwareUnitOfWorkFactory<?> unitOfWorkFactory) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCalled right after theHandleis closed.protected voidCalled right afterHandle.commit()has been called.protected voidafterRollback(Exception cause) Called right afterHandle.rollback()has been called.protected voidCalled right beforeHandle.commit()is called.protected voidbeforeRollback(Exception cause) Called right beforeHandle.rollback()is called.voidcommit()Commit theUnitOfWorkand any underlying transaction - seeUnitOfWorkStatus.CommittedThe cause of a Rollback or aUnitOfWork.markAsRollbackOnly(Exception)org.jdbi.v3.core.Handlehandle()voidmarkAsRollbackOnly(Exception cause) <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
-
Constructor Details
-
GenericHandleAwareUnitOfWork
-
-
Method Details
-
start
public void start()Description copied from interface:UnitOfWorkStart theUnitOfWorkand any underlying transaction- Specified by:
startin interfaceUnitOfWork
-
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
-
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
-
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
-
handle
public org.jdbi.v3.core.Handle handle()Description copied from interface:HandleAwareUnitOfWork- Specified by:
handlein interfaceHandleAwareUnitOfWork- Returns:
- the
Jdbihandle associated with the currentUnitOfWork
-
afterClosingHandle
protected void afterClosingHandle()Called right after theHandleis closed. Perform any additional clean-ups by overriding this method -
afterCommitting
protected void afterCommitting()Called right afterHandle.commit()has been called. -
beforeCommitting
protected void beforeCommitting()Called right beforeHandle.commit()is called. -
afterRollback
Called right afterHandle.rollback()has been called.- Parameters:
cause- the cause of the rollback
-
beforeRollback
Called right beforeHandle.rollback()is called.- Parameters:
cause- the cause of the rollback
-