How do I share a Hibernate SessionFactory across web applications?

I have two web applications that are running on a single Tomcat server and are connected to the same database with Hibernate. I am concerned that having two SessionFactory instances running around might cause some issues.

Also, since both web applications share much of the same application logic, I thought it would be a good idea to centralize as much as I could. And since I use Spring for DI and Hibernate configuration it would make sense to have a single ApplicationContext as well.

How would I go about doing something like this? Do I need to deploy a headless WAR that creates an ApplicationContext and thus a SessionFactory and allow each application access to it? Is this even a good idea?

Answers


It is possible to share the SessionFactory through JNDI (ideally, you would deploy Hibernate in your application server as a JMX-service).

But honestly, having 2+ SessionFactory instances is not really a concern, there is nothing to really worry about. This is a very common scenario, even for a single application (in a clustered environment).


I am concerned that having two SessionFactory instances running around might cause some issues.

Web applications are shielded from one other in dedicated ClassLoader. So I don't see how a clash could happen and would simply not bother with that. Two SessionFactory should be fine.


You can put your application logic that creates the Sessions, and accesses it into a shared classloader (generally a JAR in /shared) however - be very careful doing so as it is easy to generate odd exceptions unless you're familiar with what you're doing.

It's also unlikely that you'd see a lot of benefit to this (since you have to worry about syncing webapp Session state, etc.) unless you need to sync transactions across 2 webapps - in which case JTA or other options are probably still a better solution.


Need Your Help

Why is python / django returning a unicode list after split()

python database django unicode split

I have a form in Django with a TextField that receives comma separated input e.g. test1,test2,test3,test4, which is then split like such:

Cannot Connect Syncope with Apache DS

ldap apacheds apache-syncope

I am facing an error while trying to add an apache DS backend to apache syncope.