简体   繁体   中英

Singleton objects and Java servlets

When I access a singleton Java object from withing a servlet, that object is only "single" or "one instance" for the give servlet thread or single throughout the JVM on the Server OS (like Linux)?

I mean when client connects to the servlet/service, are singleton object are unique to each thread created for each client or its unique throughout the whole JVM installed in the machine?

I think the object is unique for each user, not to the whole JVM. The only persistent information is the one you put in the user session.

I'm not sure, but I think you can acomplish to have one instance of a Class in the whole Application Server using the ClassLoader class , however I don know how it's done.

UPDATE:

Taken from the Java Article "When is a Singleton not a Singleton?"

Multiple Singletons Simultaneously Loaded by Different Class Loaders When two class loaders load a class, you actually have two copies of the class, and each one can have its own Singleton instance. That is particularly relevant in servlets running in certain servlet engines (iPlanet for example), where each servlet by default uses its own class loader. Two different servlets accessing a joint Singleton will, in fact, get two different objects.

Multiple class loaders occur more commonly than you might think. When browsers load classes from the network for use by applets, they use a separate class loader for each server address. Similarly, Jini and RMI systems may use a separate class loader for the different code bases from which they download class files. If your own system uses custom class loaders, all the same issues may arise.

If loaded by different class loaders, two classes with the same name, even the same package name, are treated as distinct -- even if, in fact, they are byte-for-byte the same class. The different class loaders represent different namespaces that distinguish classes (even though the classes' names are the same), so that the two MySingleton classes are in fact distinct. (See "Class Loaders as a Namespace Mechanism" in Resources.) Since two Singleton objects belong to two classes of the same name, it will appear at first glance that there are two Singleton objects of the same class.

I'd say it depends on how the singleton is implemented, but all requests for a given app execute in the same VM, so it should be one instance for all requests.

EDIT: This is assuming a straight-forward Singleton implementation similar to:

public class MySingleton {
    private static MySingleton _instance;

    // for sake of simplicity, I've left out handling of 
    // synchronization/multi-threading issues...
    public static MySingleton getInstance() {
        if (_instance == null) _instance = new MySingleton();
        return _instance;
    }
}

Yes it is Singleton . But the scope of the singleton depends on where the class is located.

If it is located inside application, it is singleton for that application. If the same class is present inside another application then another object is created for that application and it is singleton for that application.

If it is located outside application and within server, it is singleton for the VM.

According to this

https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

every Web application has it's own class loader, so the singleton will be unique for one application and it can't be visible to another

When you create a Singleton instance, all request instances will share the same instance for the current class loader it uses.

Since all the Servlets run on a webserver which runs on a single JVM , there will be only a single Singelton Object for all your servlets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM