3

We have a portal application with one Main web app context and many minor web app contexts - plugins. Currently (very simplified) the Main one has own spring libraries and plugins would have to have them also if they wanted to use spring. In common/shared tomcat context there are just drivers and interfaces.

Would it work if spring libraries were moved to common context in regards to other libraries that spring might indirectly use or they might use spring ? Like hibernate, because the apps are using spring-tx etc. Would hibernate have to move to common/shared context too ?

What do you think, what are the other aspects ? From spring application context point of view it would be much easier like this.

lisak
  • 21,611
  • 40
  • 152
  • 243

3 Answers3

0

If you were able to move from Tomcat to a full-blown Java EE container then an option would be to package everything as an EAR using the Bundled Optional Classes mechanism.

You'd then move the common JARs out of the WARs & into the top level of the EAR.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Richard Barnett
  • 1,078
  • 9
  • 13
0

@RichW is correct in stating that placing Spring libraries in Tomcat's common classloader is bad practice. And there's a good chance it won't work.

Java uses a classloader hierarchy). When a class load is requested, the classloader will recursively request the class from it's parent classloader before attempting to load the class using it's own classpath. This process continues up to the root classloader (know as the bootstrap classloader). In this way, classes referenced from a parent classloader always get priority over classes referenced in classloaders further down the hierarchy.

It's important to note that in this process classes are never loaded from a child classloader. Therefore any classes required by Spring would also need to be loaded into the common classloader - including asm, log4j, commons-logging and cglib (all of which spring depends on). This will lead to a whole host of problems: in particular, including commons-logging in the common classpath is a whole world of hurt

If you actually managed to get Tomcat started, then you would experience problems with memory leaks when recycling applications. In tomcat, applications are unloaded using conventional garbage collection, so if anything holds a reference to a class inside an application which has subsequently been restarted, that application will not get garbage collection. Spring and logging frameworks are prime candidates for holding references to classes so you will probably suffer from OOM errors after a few application restarts.

The only way to do this safely would be to consider using a full blown application server (such as JBoss AS) and deploy your application as an EAR.

Andrew Newdigate
  • 6,005
  • 3
  • 37
  • 31
-1

Yes, I know it's tempting. Yes, it can work. But putting application-specific or framework-specific libraries in the shared libraries folder of an app server is considered by some to be a bad practice, and I agree.

In my opinion web-apps should contain their own dependencies (app jars, framework jars, etc.). Frameworks also have dependencies, often requiring multiple jars with particular versions. Sometimes these versions change, sometimes the dependencies change. Over time that shared library folder will become a kitchen sink for jars, and that will affect all your apps, perhaps in unpredictable ways.

Going the shared library folder route you gain some slight initial convenience, but what you lose is choice: the choice to only affect one web-app at a time. I recommend you keep your jars within your web-app, nicely contained and separate from the other web-apps. It will make them more reliable and you'll find framework upgrades easier to handle. You'll be happier in the long run, I promise you.

RichW
  • 2,004
  • 2
  • 15
  • 24
  • 3
    Your answer is entirely general as to whether or not having libraries in common/shared context...I'm not talking here in general terms. I mentioned Spring framework specifically. It has no third party dependencies and if almost each of the plugins had to load its own Spring, it would make such a huge memory footprint, that dealing with version conflicts would be lesser evil. The portal is composed of those plugins. I was hoping that it is self explaining if I call it plugin... – lisak Sep 20 '11 at 18:35