• Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with Singleton Class  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have an issue where I am using a Singleton class to manage my properties for a custom application. However if I create two instances of my app, the static value for second instance overwrite that of first instance..... (as expected I suppose..)

Is there any way I can use two instances together?

Has anyone solved a similar problem before?

If possible, I don't want to rewrite code to pass a non-static Properties object throughout all classes.
Doesn't anyone know if I use different class loaders for each instance would it work?

I have written a simple application to write message to a MQ Message Server. I initialise application with a Properties object.. some psuedo code.... Similarily to receive a message I have simple code:

In case a above, instance work fine together however .....

.. breaks because sender properties have been over written.....

Please advise.....
Any suggestions really appreciated
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knowing the applications classloading hierarchy might help in this case.

If two classloaders are used to load the two properties file then your problem might be solved.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... just one reason to avoid Singleton pattern!

Using separate ClassLoaders per sub-application within an application can resolve such problems. However, it is quite hard to get it right. If the sub-applications have to communicate directly via Java at all, it gets even harder.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I asked the initial poster to get familiar with the classloading hierarchy is mentioned by Peter Chase.

For example , If you have a singleton class and its being used as part of four web applications and all of them are deployed in Tomcat and the class file of the singleton is present in the lib directory of all the web applications then the class would be loaded four times using webapp classloader of Tomcat and hence no issues.

I do not have any idea about MQ Message server.But in case of EJB deployed in clustered environment (thought not directly related to the actual question) singleton should be avoided.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rahul and Peter said, i think it has to deal with the ClassLoading Mechanism of the application being dealt with.

Its better to check with MQ and its internals as it may vary in terms of class loading.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eddie Howard:

If possible, I don't want to rewrite code to pass a non-static Properties object throughout all classes.


I would highly recommend to consider this option. It might look like a lot of work, but I seriously think that it is the option that

- is the easiest to understand
- is the one with lowest risk of introducing hard to find and fix bugs, and
- gives you the most flexibility in the future

The last point is not to be underestimated. Changes tend to cluster - if you now have to do a change for which the Singleton is making things harder, it's likely that it will happen again in the future. Better get rid of it immediately.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!