views:

246

answers:

1

Greetings, I'm working on a project that have several webapps (WARs) built with Maven and deployed in a JEE.

These WARs share several common business JARS (like one containing domain objects which are loaded from hibernate) and other framework JARs like Spring and Hibernate.

They use Spring MVC, and the Application Context loads Hibernate. As each WAR has its own Classpath in the servlet container, the Hibernate cache (EHcache) is not shared.

What I'd like is to share the cache and also the hibernate session factory bean (as well as other common beans) betweeen the different WARs. I think this is possible by repackaging those WARs inside an EAR and then I'd have to make a spring configuration XML using those commons beans and in the WAR's Spring XML use something like SingletonBeanFactoryLocator from what I've read.

What I'm asking here is if there is a simple way to do this, minimizing changes to the WARs' POMs

Note: I'm familiar with WARs, tomcat and servlets, but not so much with EARs.

Thanks in advance.

A: 

Hmmm... Most Java EE containers use isolated classloaders for WARs, even in an EAR (even if the Java EE spec does not mandate class loading isolation among modules of a single EAR) so I wouldn't expect to much from an EAR packaging, especially if you want your application to remain portable (i.e. if you don't want to rely on any app server specific behavior).

Now, if really it makes sense to share your session factory and your 2nd level cache between several applications, maybe consider merging them in a single WAR. That would be the easiest way IMO. But I'd be tempted to ask why are they separate then? When applications are separate, they have most of time separate governance and I don't know if deploying them together would be a good idea in such case.

And if merging the WARs is not an option, please tell us which container you are using.

Pascal Thivent
I'm not sure why they are separated. That was the way before I started working there, and the first time I've seen that. They told me that is in case some day they decide to put the war in different servers. But I don't think that will happen in the foreseeable future, and now we are using hibernate more heavily and was just worried about memory issues (of course I have to profile first).We are using Jboss 4.0.5
Luciano
@Luciano If they are sharing the same domain model, if they have the same life cycle, it would make sense (and you'll indeed save some memory). In that case, I'd see if merging them is possible (even at the code level if possible, you are in a typical YAGNI situation here). I'll update my answer to cover the case of JBoss though.
Pascal Thivent