aspose file tools*
The moose likes Servlets and the fly likes Java Servlers/Beans and singleton objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Java Servlers/Beans and singleton objects" Watch "Java Servlers/Beans and singleton objects" New topic
Author

Java Servlers/Beans and singleton objects

Eric Grobler
Greenhorn

Joined: Nov 07, 2011
Posts: 5
Hallo Java experts,

I am new to Java and I am not sure if there are issues relating to singleton classes in servlets/beans especially in a clustered environment.
Here is a simple example of a hashmap in a singleton class called GlobalModuleInfoList and both a servlet and a bean registers themselves and access it.

My question is therefore if it is an issue if the bean access a global singleton class (as per this example).
I could of course put the hashmap directly in the bean and allow multiple servlets access to it, but I have non Java EE code that also need access to a global registered module list.




Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Eric Grobler wrote:if there are issues relating to singleton classes in servlets/beans especially in a clustered environment.


There are lots of issues relating to singleton classes. Period. Singletons Considered Harmful. There are issues before you get to the complexity of clustered environments.

What, precisely, is the nail that you are trying to hammer with the singleton pattern?
Eric Grobler
Greenhorn

Joined: Nov 07, 2011
Posts: 5
Hi Pat,

Thank you for your reply.

What, precisely, is the nail that you are trying to hammer with the singleton pattern?

I understand that singletons should be avoided and the usage is often the result of bad design.
I want to report statistics and information based on class usage in different parts of my code.

There are issues before you get to the complexity of clustered environments.

Are there some standard java libraries or popular third party libraries that should be avoided in Java EE Servers for this reason?

I am interested to know what the major issues you are referring too are.
If we ignore the "bad design" aspect of singleton's what will be the difference to a java ee server if you use a singleton bean vs a singleton object to hold distinct information about classes as per example below.

Thank you
Eric





Eric Grobler
Greenhorn

Joined: Nov 07, 2011
Posts: 5
I found a response from Mike Baranczak relating to a similar post.

If you're talking about the standard singleton pattern, where you keep the instance in a static variable, then that's a bad idea. You should generally avoid static variables in Java EE or servlet containers, because the class loading can be a bit tricky - you may wind up with multiple copies when you don't expect it, or you may be sharing a single copy between different applications, or you may be keeping stuff in memory when you redeploy your application. You can make an exception in cases where the variable isn't exposed outside the class, and you don't really care how many copies of it you have (for example, logger objects).


I wonder if the multiple instance issue would be solved if one would use Joshua Bloch's enum method:



Regards
Eric
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

I would expect an enum to have basically the same class-loading issues as a static variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Servlers/Beans and singleton objects