Qi4j

UnitOfWork Library

code
docs
tests
4.0.0 org.qi4j qi4j-library 0.7 org.qi4j.library qi4j-lib-unitofwork Qi4j Library - UnitOfWork 0.8-SNAPSHOT org.qi4j.core qi4j-core-api junit junit

Declarative UnitOfWork Management Support


This library allows you to use a declarative approach for management of the UnitOfWork creation, completion and discarding.

Instead of manually creating, completing and discarding UnitOfWork, it is possible to declare @UnitOfWorkPropagation on methods, describing the requirements of UnitOfWork management at that method entry and exit.

For instance;

@UnitOfPropagation( UnitOfPropagation.Propagation.REQUIRES_NEW )
public Location createLocation( "HKG", "Hong Kong" )
{
    :
}
In the above case the following things will happen;
  • On method entry, a new non-nested UnitOfWork is created.
  • On method exit, the UnitOfWork is completed unless an Exception is thrown in which case it is discarded.

For this to work, you need to include the UnitOfWorkConcern on the composite where you want this support to be available.

@Concerns( { UnitOfWorkConcern.class } )
public interface MyExcellentService extends MyExcellent, ServiceComposite
{}
The values available for the @UnitOfWorkPropagation are;
  • REQUIRED, if a UnitOfWork is already in-progress it will be used, otherwise a new one is created. If one is created, then it will either be completed or discarded on method exit, but if one was already used, the method exit does nothing. This is the default value.
  • MANDATORY, if a UnitOfWork is not in-progress, then a UnitOfWorkPropagationException will be thrown, otherwise the method will proceed normally. No processing on method exit.
  • REQUIRES_NEW, a new UnitOfWork will be created, and upon method exit it will be completed or discarded.
  • REQUIRES_NESTED, if a UnitOfWork is in progress, then create a nested UnitOfWork, otherwise create a new UnitOfWork. On method exit, complete or discard the UnitOfWork created.
  • null, a UnitOfWorkPropagationException is thrown.

It is also possible to declare the Discard Policy with two annotations;

  • @UnitOfWorkDiscardOn, whose value is an array of Throwable classes. If the thrown class is a subclass of any class in the array, then discard the UnitOfWork.
  • @UnitOfWorkNoDiscardOn, whose value is an array of Throwable classes. If the thrown class is a subclass of any class in the array, then do NOT discard the UnitOfWork.

If both of those annotations are present, then the discard will only happen iff the thrown class is a subclass of any of the @UnitOfWorkDiscardOn classes and not present among the classes in the @UnitOfWorkNoDiscardOn classes.


Qi4j and the Qi4j logo are trademarks of Richard Öberg, Niclas Hedhman and the members of the Qi4j Core Team. See Qi4j licensing for more information.
Powered by SiteVisionexternal link.