Forums Register Login

Issue with Singleton Class

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

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.
I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1123 times.
Similar Threads
A Question In The Monkhouse Book
Singleton Pattern
Find THE jar containing a class Axis2
Heterogeneous map and generics
How do Singleton class? Is it even possible?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:20:01.