views:

245

answers:

3

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?

+1  A: 

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.

ewernli
+1  A: 

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.

jayshao
Syncing is an issue since one of the applications is an administrative application for the client application. Thanks for the response. I agree that putting application stuff in a shared classloader is probably not a great idea.
Jeremy
Yeah, unfortunately the JNDI route basically requires that too - same as JDBC drivers.
jayshao
+1  A: 

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).

Pascal Thivent
JNDI is a thought, but that might not be do-able at this point. I will look into it more. Your second point makes a lot of sense, though. Thank you!
Jeremy