Forums Register Login

Java Servlers/Beans and singleton objects

+Pie Number of slices to send: Send
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.




+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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





+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
I would expect an enum to have basically the same class-loading issues as a static variable.
Uh oh, we're definitely being carded. Here, show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1485 times.
Similar Threads
i have a private constructor and a public constructor
Hibernate InheritanceType.SINGLE_TABLE with different eager OneToOne relations
Singleton Class ?
Instance of Singleton pattern
To Make a HashMap as a Singleton
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:31:17.