Class TransactionalRequest

java.lang.Object
io.jooby.hibernate.TransactionalRequest
All Implemented Interfaces:
Route.Aware, Route.Filter

public class TransactionalRequest extends Object implements Route.Filter
Attaches a Session and EntityManager to the current request via SessionRequest.

The route pipeline runs inside a transaction which is commit on success or rollback in case of exception.

Applies the SessionRequest decorator, so there is no need to use session request in addition to transactional request.

Usage:


 {

   install(new HikariModule());

   install(new HibernateModule());

   use(new TransactionalRequest());

   get("/handle", ctx -> {
     EntityManager handle = require(EntityManager.class);
     // work with handle.
   });
 }
 
Since:
2.0.0
Author:
edgar
  • Constructor Details

    • TransactionalRequest

      public TransactionalRequest(@NonNull String name)
      Creates a new transactional request and attach the to a named session factory.
      Parameters:
      name - Name of the session factory.
    • TransactionalRequest

      public TransactionalRequest()
      Creates a new transactional request and attach to the default/first session factory registered.
  • Method Details

    • enabledByDefault

      public TransactionalRequest enabledByDefault(boolean enabledByDefault)
      Sets whether all routes in the scope of this decorator instance should be transactional or not (true by default).

      You can use the Transactional annotation to override this option on a single route.

      Parameters:
      enabledByDefault - whether routes should be transactional by default
      Returns:
      this instance
      See Also:
    • apply

      @NonNull public Route.Handler apply(@NonNull Route.Handler next)
      Specified by:
      apply in interface Route.Filter