• 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

use singleton utility class in EJB

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

If my EJB wants to use a singleton utility method. What should be considered?

The creation of my singleton getInstance() method will have a synchronized on it, does this violate EJB spec. as it forbids the use of synchronization in EJB?

If this is OK, would that be a performance issue since all calls have to synchronize on this creation method.

What would be a good way of doing that?
Thanks.
Yan
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yan,
I assume we are discussing a singleton class, not just a method?

Take a look at the double checked locking pattern. It removes the performance issue because most callers won't hit the synchronized part.
 
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 Yan,

In my opinion is nothing wrong with a singleton that synchronies the getInstance() method. Singletons are actually used by other tools like spring or hibernate. The service locator pattern relies upon it as well. They still have a lot of value if the developers don't abuse them.
As for the double check locking pattern, it has its own drawbacks as well and if I remember correctly there are articles on the net that prove that it can fail as well in some circumstances (sorry Jeanne). Better use the synchronized getInstance(); just my opinion.
Regards.
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The creation of my singleton getInstance() method will have a synchronized on it, does this violate EJB spec. as it forbids the use of synchronization in EJB?



No it doesn't. If that was the case Vector class would have been in the EJB world.
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yan,
The EJB specification also discourages the use of static fields in EJB classes -- and also explains why (see section 25.1.2 of the EJB 2.1 specification).

I think the use of singleton classes should also be discouraged for the same reason. The scope of a singleton class is its class loader. Since you cannot be sure that all your EJBs will reside in the same class loader, you cannot be sure that you truly have a singleton object. However, if your singleton objet does not need to maintain any state, the above restriction is probably irrelevant.

Good Luck,
Avi.
 
Get me the mayor's office! I need to tell her about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic