GeeCON Prague 2014*
The moose likes EJB and other Java EE Technologies and the fly likes use singleton utility class in EJB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "use singleton utility class in EJB" Watch "use singleton utility class in EJB" New topic
Author

use singleton utility class in EJB

Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30598
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
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.


I think, therefore I exist -- Rene Descartes
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1134

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use singleton utility class in EJB