public interface UnitOfWork
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
:
{
decorator(new TransactionalRequest());
// will lead to exception
get("/nope", ctx -> require(UnitOfWork.class)
.apply(em -> em.createQuery("from Pet", Pet.class).getResultList()));
}
Modifier and Type | Interface and Description |
---|---|
static interface |
UnitOfWork.TransactionHandler
Allows committing or rolling back the current transaction, immediately
beginning a new one.
|
Modifier and Type | Method and Description |
---|---|
default void |
accept(SneakyThrows.Consumer<javax.persistence.EntityManager> callback)
Runs the specified code block passing a reference to an
EntityManager to it. |
default void |
accept(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(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(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. |
default void accept(SneakyThrows.Consumer<javax.persistence.EntityManager> callback)
EntityManager
to it.callback
- the block to executedefault void accept(SneakyThrows.Consumer2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler> callback)
EntityManager
and
a UnitOfWork.TransactionHandler
to it.callback
- the block to executedefault <T> T apply(SneakyThrows.Function<javax.persistence.EntityManager,T> callback)
EntityManager
to it,
and returns it's result.T
- type of return valuecallback
- the block to execute<T> T apply(SneakyThrows.Function2<javax.persistence.EntityManager,UnitOfWork.TransactionHandler,T> callback)
EntityManager
and
a UnitOfWork.TransactionHandler
to it, and returns it's result.T
- type of return valuecallback
- the block to executeCopyright © 2022. All rights reserved.