0

I have some Web-Apps. Each in different v-hosts on Tomcat7. All this Web-Apps use the same collection of Librarys (written by my own), stored in WEB-INF/lib as .jar This Library has some static Classes (Logger, Config, etc). It seams web-app X can see/use the static Instance of web-app Y. "randomly" X writes in the logging-file of Y. Y uses configs from X.... etc.

Is this generaly a problem with the JVM(s) in Tomcat ?

Only for the Servlets i can store the static Classes in the ServletContext, but Non-Servlets cant reach them, right ?

Here the Constructor in Class Config.java

public class Config{
    public static Config instance;

    private Config(){

    }
    public static Config getInstance(){
         if(instance==null) instance = new Config();
         return instance;
    }
}

In Servlet and also in other Classes i use

private static Config config = Config.getInstance();

Is there any other way to share ONE instance of a Class in the whole Web-App but only in THIS web-app ?

Slacki
  • 1
  • what do you mean "whole Web-App" ? – Stultuske Apr 29 '15 at 18:59
  • "Capital S Singletons" are an anti-pattern: http://stackoverflow.com/questions/11292109/why-implementing-a-singleton-pattern-in-java-code-is-sometimes-considered-an-a You should use dependency injection to inject an instance of Config into your servlets. – eakst7 Apr 29 '15 at 19:03
  • This answer should give you the information you are looking for: http://stackoverflow.com/a/17721573/16959, basically a Singleton is isolated per ClassLoader, and Tomcat loads a separate ClassLoader per web app. If you put your singleton code in a Jar inside the `tomcat/libs` folder then it will get loaded into the global ClassLoader that is common to all Web Apps on that instance of Tomcat. – Jason Sperske Apr 29 '15 at 19:05
  • the lib contains my "helpers" i use for all projects. i cant store it in tomcat/libs becouse each web-app-project needs his own helper-lib (becouse the static classes inside the lib). so each prj. needs his own lib, but the static inside should only be visible in this project. but the "whole" project.... servlets and other classes – Slacki Apr 30 '15 at 04:53

1 Answers1

0

Anything running in context /X cannot see anything in /Y/WEB-INF/lib or Y/WEB-INF/classes. The reason is that the jars in /X/WEB-INF/lib and classes in /X/WEB-INF/classes are in a different classloader than the jars and classes for context /Y. This is not a Tomcat "problem", this is per the J2EE servlet spec.

I would revisit my design, but if you really need to share a class between two or more contexts, put the classes in a jar, and place the jar in ${CATALINA_HOME}/lib. Classes in that folder are visible to all contexts, as they are loaded in a classloader that is the parent of the context classloaders.

The "correct" way to do this is to register the object in JNDI, where every component of your app can look it up.

Tony BenBrahim
  • 7,040
  • 2
  • 36
  • 49
  • thanks Tony, but i want to share a singleton in the whole web-app-project (servlet and non-servlet) NOT with all other web-app-prj. ${CATALINA_HOME}/lib would share bith all other projects ?! – Slacki Apr 30 '15 at 04:46
  • Have you considered putting your singleton in JNDI and looking it up that way, that is what it is designed for (edited my answer) – Tony BenBrahim May 01 '15 at 20:41