Repackage several WARs into one EAR

I’m working on a project that have several webapps (WARs) built with Maven and deployed in a Java EE.

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.

Answer

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.

Leave a Reply

Your email address will not be published. Required fields are marked *