• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Singleton and Thread Safety

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am thinking about making my DAOFactory a singleton and reading following article about singletons:

http://c2.com/cgi/wiki?JavaSingleton

which led to another article:

http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html

Honestly, it got me more confused that informed about how to implement my singleton DAOFactory.

Any thoughs?

PI
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ingle,
First of all singletons are not the best choice with j2ee applications. One obvious reason could be the clustered environments, where you�ll have one singleton per server instance.
Having said that, here there is my general opinion: synchronize the getInstance() method in your singleton class and that should work, like in this example (got it from the same site):

And here there are my thoughts:
The problem that the Double-Checked Locking tries to solve is the inefficiency of synchronizing the whole method. Well let me tell you what I think: there is no inefficiency here, because this method takes exactly 0ms to execute; it runs so fast that the execution time is practically un-measurable. Next question to ask is how many concurrent threads will run this piece of code "simultaneously" and how long the delay would be? As you might guess this is also limited by the number of threads that the jvm was configured to use (which could be around 70, in order for the setting to become contra-productive). As for the delay... I expect it to be un-measurable as well. You can also run several loading tests and make sure that this is not a bottleneck in your application.
Regards.
 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Valentin Tanase:

First of all singletons are not the best choice with j2ee applications. One obvious reason could be the clustered environments, where you�ll have one singleton per server instance.



what are the pitfalls of that?

Secondly, if not Singletons, is Static or final class my other choice?

I have this situation regarding two classes :
1. DAOFactory which will be creating stateless DOAs and
2. utility class that hads out database connections

Should there be inly one instance (or static methods avaiable) of these classes and if so what is the best way to implement these - singleton or static / final class?

Thanks,
P Ingle
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote]
what are the pitfalls of that?

Let me give you an example. Let�s suppose that one decides to design a class for generating unique numbers. Latter on s/he intends to use this class for generating pk for the entity ejbs. Having a singleton will make sense since the numbers must be unique throughout the application. As you might guess this will work just fine when the app will be deployed to a server, but will fail in a clustered environment.


Secondly, if not Singletons, is Static or final class my other choice?


You might consider singletons as well as long as you don�t face problems similar to the one above. There are also good debates whether to use singletons vs static classes and you might search the net for that. With our design we implemented the DAOFactory as a static class.
Regards.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic