• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Servlers/Beans and singleton objects

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would expect an enum to have basically the same class-loading issues as a static variable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic