This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use singleton utility class in EJB

 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • 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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • 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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic