Mar 17 2012

Using Hibernate with Mule ESB – Part 2

In our previous post we explained how to work with Mule and Hibernate using an interceptor that makes every method invocation transactional. In this post we will show how to simulate the open session in view pattern (used in Web apps) for each flow.

To do so, we will assume that we have the following simple flow that receives messages from a HTTP endpoint and chains 3 services in a flow:

We want to reuse the open session in view concept for our HTTP flow. To do so, I have created 2 MessageProcessor that opens and closes the hibernate session. Their implementation is as follows:

and:

The HibernateSessionAdvice class is an adapted copy of OpenSessionInViewFilter; you can find the implementation here. The last step is to modify our HTTP endpoint to open and close the hibernate session using the previously created MessageProcessor. We modify our configuration and flow as follows:

Hope it helps and happy hacking!


Oct 28 2011

Using Hibernate with Mule ESB – Part 1

If you are looking to use Hibernate with Mule ESB this is your place. Though Mule has a Hibernate transport, it is out date and it won’t work with Mule 3.
In this series we are going to explain different scenarios where you can integrate these technologies.

Let’s begin with something simple; suppose we are building an application that reads from a JMS queue and you would like to invoke a method in MyService Spring bean. Let’s assume we have the following Spring configuration:

and it’s implementation is as follows:

following our implementation you may try the following configuration for our scenario (assuming that the elements in the queue are long paramenters):

This implementation assumes that a Hibernate session exists for the current Thread; something typical in Web applications using the Open session in view pattern. Well in this case it won’t work because nobody has created a Hibernate session for you.

How to fix it?

There are several ways, in this case we are going to open the session and make the invocation transactional. To do so, first create 2 interceptors: a transaction interceptor and the hibernate interceptor.

Then you can refactor the Spring config as follows using the Spring’s ProxyFactoryBean:

Finally you will need to add a default constructor to be able to proxy your com.MyService class as follows:

Hope you like the post and stay tuned; we will present more alternatives to use Hibernate with Mule soon…