0

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?

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I once tried this with some RESTful webservices expecting to have some "State" and it was not possible, the Singleton was new for each connection.
Whoa, all other answers say different. It would be nice to see some working example. I'm answering based on my personal experience.
The link to the appears broken; it redirects to the Java technical page.
Thank you @Tragedian, I have fixed it, however, that's why I copied and pasted the content of the article right below the link :-)
3

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;
    }
}

5 Comments

How do I make the singleton implementation that is unique only the the client request?
Then you don't need a singleton - what are you trying to do?
I'm trying to create a "Manager" class that will contain some values based on Http client operations. That the values should not intermingle with other Http client operations.
I think your initial question has been answered (scope of Singletons). Perhaps you should create a new question describing what you're trying to achieve and what problems you encounter, and we can help you there.
It depends where the class is present. If the class is present in WEB-INF/lib of each application. Singleton will be created for each application.
1

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.

Comments

1

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

Comments

0

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

5 Comments

So you mean, all client will get the same data that is stored in the Singleton class, if ever there is stored?
Yes,It will assure to have same instance will be shared by the all clients
How do I implement a singleton implementation that is unique only to the client request, or I just need to make a static class on the servlet?
The implementation pf Singleton is usually done by static.Do not make servlet static.Whatever objects you need a single instance and is shared by other clients, create it using static
@xybrek for how to implement a Singleton, check the link I gave in my answer, however I stand by my answer that it will not be a Single Object if done in a Servlet.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.