Interface UnitOfWork


public interface UnitOfWork
Allows you to open a JPA session on demand by acquiring an instance of a class implementing this interface via the service registry or DI.

Usage:


 {
   get("/pets", ctx -> require(UnitOfWork.class)
       .apply(em -> em.createQuery("from Pet", Pet.class).getResultList()));
 }
 
Automatically manages the lifecycle of the EntityManager instance and transactions. After the code block passed to apply or accept returns the transaction is being committed and the EntityManager closed.

If the code block throws an exception, the transaction is rolled back, and the EntityManager is released as well.

You may access a UnitOfWork.TransactionHandler instance to be able to work with multiple transactions:


 {
   get("/update", ctx -> require(UnitOfWork.class)
       .apply((em, txh) -> {
         em.createQuery("from Pet", Pet.class).getResultList().forEach(pet -> {
           pet.setName(pet.getName() + " Updated");
           txh.commit(); // update each entity in a separate transaction
         });
         return "ok";
       }));
 }
 
A call to UnitOfWork.TransactionHandler.commit() commits the current transaction and automatically begins a new one. Similarly, you can issue a rollback using UnitOfWork.TransactionHandler.rollback() which also begins a new transaction after rolling back the current one.

UnitOfWork does NOT allow nesting:


 {
   get("/nope", ctx -> require(UnitOfWork.class)
       .apply(em -> {

         // will lead to exception
         require(UnitOfWork.class).accept(...);

         return "ok";
       }));
 }
 
Neither can it be used together with SessionRequest or TransactionalRequest:

 {
   use(new TransactionalRequest());

   // will lead to exception
   get("/nope", ctx -> require(UnitOfWork.class)
       .apply(em -> em.createQuery("from Pet", Pet.class).getResultList()));
 }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Allows committing or rolling back the current transaction, immediately beginning a new one.
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    accept(io.jooby.SneakyThrows.Consumer<javax.persistence.EntityManager> callback)
    Runs the specified code block passing a reference to an EntityManager to it.
    default void
    accept(io.jooby.SneakyThrows.Consumer2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler> callback)
    Runs the specified code block passing a reference to an EntityManager and a UnitOfWork.TransactionHandler to it.
    default <T> T
    apply(io.jooby.SneakyThrows.Function<javax.persistence.EntityManager,T> callback)
    Runs the specified code block passing a reference to an EntityManager to it, and returns it's result.
    <T> T
    apply(io.jooby.SneakyThrows.Function2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler,T> callback)
    Runs the specified code block passing a reference to an EntityManager and a UnitOfWork.TransactionHandler to it, and returns it's result.
  • Method Details

    • accept

      default void accept(io.jooby.SneakyThrows.Consumer<javax.persistence.EntityManager> callback)
      Runs the specified code block passing a reference to an EntityManager to it.
      Parameters:
      callback - the block to execute
    • accept

      default void accept(io.jooby.SneakyThrows.Consumer2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler> callback)
      Runs the specified code block passing a reference to an EntityManager and a UnitOfWork.TransactionHandler to it.
      Parameters:
      callback - the block to execute
    • apply

      default <T> T apply(io.jooby.SneakyThrows.Function<javax.persistence.EntityManager,T> callback)
      Runs the specified code block passing a reference to an EntityManager to it, and returns it's result.
      Type Parameters:
      T - type of return value
      Parameters:
      callback - the block to execute
      Returns:
      the result of the callback
    • apply

      <T> T apply(io.jooby.SneakyThrows.Function2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler,T> callback)
      Runs the specified code block passing a reference to an EntityManager and a UnitOfWork.TransactionHandler to it, and returns it's result.
      Type Parameters:
      T - type of return value
      Parameters:
      callback - the block to execute
      Returns:
      the result of the callback