Data Access Service

../../_images/example-gateway.png

Data Access Service

Introduction

This initial version of tutorial shows how to use Hibernate with Baratine

Hibernate can be used efficiently with Baratine in a data lookup service or data update service.

Data Lookup Service

In Data Lookup Service Hibernate should be initialized in a Service Method annotated with @OnInit. @OnInit annotoated method is called before the Service becomes operational and makes for perfect place to configure Hibernate Session Factory.

Method annotated with @BeforeBatch is where Hibernate session can be open. Once the session is open it can serve multiple requests before it is closed in @AfterBatch.

@OnDestroy is an annotation that marks a method called before Baratine service is destroyed. Closing Hibernate Session Factory can be done inside this method.

@OnInit

Begins a lifecycle of a Service:

@OnInit
public void init()
{
  //build Hibernate Session Factory
  Configuration config = new Configuration();
  config.configure("hibernate.xml");
  _factory = config.buildSessionFactory();
}

@BeforeBatch

Starts processing of a batch of messages (calls to a service method):

@BeforeBatch
public void beforeBatch()
{
  _session = _factory.openSession();
  _session.getTransaction().begin();
}

Service Method

List countries:

public Country[] list()
{
  Query query = _session.createQuery("SELECT c FROM country.Country as c");

  return query.list();
}

@AfterBatch

Signals that the Service inbox is emptied, perfect place to close Hibernate Session:

@AfterBatch
public void afterBatch()
{
  _session.getTransaction().rollback();
  _session.close();
}

@OnDestroy

Signals that the service is about to be destroyed; the Session Factory can be closed at this point:

@OnDestroy
public void destroy()
{
  _factory.close();
}

Source Code for the example

A complete Maven based sample for this tutorial can be found at githib.

Please refer to http://github.com/baratine/example-service-with-hibernate for source.

Data Lookup Service with Workers

@Workers annotation instructs Baratine to use multiple instances of a Service. The upper limit of a number of instances Baratine will use is specified in a @Workers’s value. Optimal value should be determined experimentally.

Workers are useful when there is a blocking operation involved e.g. requesting data from a database or a remote service, like stock quote or currency exchange lookup.

This examples builds on the previous example.

Annotate the service with @Workers:

@Service("local:///hibernate-country")
@Workers(3)
public class CountryHibernateWorker
{
  private @Inject HibernateSessionFactoryManager _sessionManager;
  private Session _session;
  ...
}

Since there needs to be a single instance of Hibernate SessionFactory application needs an @ApplicationScoped class to manage SessionFactory:

@ApplicationScoped
@io.baratine.core.Startup
public class HibernateSessionFactoryManager
{
  SessionFactory _factory;
  Session _session;

  @PostConstruct
  public void init()
  {
    Configuration config = new Configuration();
    config.configure("hibernate.xml");
    _factory = config.buildSessionFactory();
  }

  public Session openSession()
  {
    return _factory.openSession();
  }

  @PreDestroy
  public void destroy()
  {
    _factory.close();
  }
}

With the instance of HibernateSessionFactoryManager injected into CountryHibernateWorker, the worker is ready to start servicing the requests:

@BeforeBatch
public void beforeBatch()
{
  _session = _sessionManager.openSession();
  _session.getTransaction().begin();
}

public Country[] list()
{
  Query query = _session.createQuery("SELECT c FROM country.Country as c");

  List list = query.list();

  Country[] countries = new Country[list.size()];

  for (int i = 0; i < list.size(); i++) {
    Object o = list.get(i);
    countries[i] = (Country) o;
  }

  return countries;
}

@AfterBatch
public void afterBatch()
{
  _session.getTransaction().rollback();
  _session.close();

  _session = null;
}

An optimal value for @Workers will have list() method firing multiple times within a batch (enclosed in @BeforeBatch and @AfterBatch).

Source Code for the Workers example

A complete Maven based sample for this tutorial can be found at githib.

Please refer to hibernate examples posted at “https://github.com/baratine/baratine-examples”:https://github.com/baratine/baratine-examples for source.