Singleton across JVM or Application instance or Tomcat instance

前端 未结 5 1190
星月不相逢
星月不相逢 2020-11-30 04:17

If I deploy and run 2 instances of same application on a single instance of Tomcat(Or any other server). Then a single object(of a Singleton class) would be created:

5条回答
  •  无人及你
    2020-11-30 04:57

    A singleton is normally tied to a ClassLoader only.

    So if you have a singleton based on a .class file in your .war file, and you deploy this web application multiple times, each application gets its own singleton.

    On the other hand, if the .class file of your singleton is in the classpath of tomcat, then you only have one instance. This .class does not belong to a specific web application (it belongs to the tomcat instance).

    If you have the singleton in both locations, it depends on the class loader hierarchy, and you can possibly select between "parent first" or "web application first".

提交回复
热议问题