org.apache.onami.persist
Interface UnitOfWork


public interface UnitOfWork

The Unit of work correlates with the life cycle of the EntityManager. According to JPA every thread should use its own EntityManager. Therefore the unit of work will control the life cycle of the EntityManager on a per thread basis. This means the UnitOfWork is thread safe.

Most of the time it is not recommended to manual control the unit of work.

For applications running in a container the PersistenceFilter is recommended. It will start a unit of work for every incoming request and properly close it at the end.

For stand alone application it is recommended to relay on the @Transactional annotation. The transaction handler will automatically span a unit of work around a transaction.

The most likely scenario in which one would want to take manual control over the unit of work is in a background thread within a container (i.e. timer triggered jobs).

Recommended pattern:

 public void someMethod() {
   final boolean unitOfWorkWasInactive = ! unitOfWork.isActive();
   if (unitOfWorkWasInactive) {
     unitOfWork.begin();
   }
   try {
     // do work
   }
   finally {
     if (unitOfWorkWasInactive) {
       unitOfWork.end();
     }
   }
 }
 


Method Summary
 void begin()
          Begins the unit of work.
 void end()
          Ends the unit of work.
 boolean isActive()
           
 

Method Detail

begin

void begin()
Begins the unit of work. When a unit of work has already been started for the current thread an IllegalStateException is thrown.

Throws:
IllegalStateException - if a unit of work is already active for this thread.

isActive

boolean isActive()
Returns:
true if the unit of work is active for the current thread false otherwise.

end

void end()
Ends the unit of work. When the unit of work is not active this method will do nothing.



Copyright © 2013–2014 The Apache Software Foundation. All rights reserved.