• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Singletons in EJB

 
Sajid Moinuddin
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I am new to ejb and I came from the spring world. However, I have some doubts about the threading policy in ejb.

First, According to best practices, I should not use synchronized blocks in ejb. Then, How do i implement singletons? Or, is it not valid to use singletons in ejb? Suppose I want to use an utility class like HomeLocator. Now, the purpose of this class is to lookup homes and do some caching. I don't want to create a new instance of this class each time i want to look up a home. So I would like to use it as a singleton. Again the block in which it's calling lookup on Context, should also be synchronized since the lookup method is not threadsafe. How do I implement these logics without using singleton and thus synchronized blocks?

Second, I want to use some kind of static method which will lookup a home interface of some ejb, find a particular component interface and do some manipulation. I will call these static methods say from my ejbCreate. Now, what will be the thread safety policy for them? I know the container is supposed to give me thread safety. But how will it give it? Will it synchronize my calls? I am a bit confused here. Should I instead make these kind of methods some instance methods of some class and create a new instance of that class each time I want to call such a method. That will give me thread safety. But This particular method does not use any instance specific information, So according to my logic, it should go into a static method.But then again, I want my calls be performed without any waiting for thread synchronization. What's the solution?

I feel kinda embarrased asking such questions. But hey, according HFEJB, there is no dull question ...
 
satheesh krishnaswamy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You can always implement singleton pattern in EJB using Business Delegates and Service Locator.

Write the Utility class for Home Lookup and from Business Delegates,Use Service Locator Design patterns to Cache EJBHome.

I can give a Example here...



Hope this Helps...
 
Sajid Moinuddin
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnx satheesh.
Now, my question is,
ServiceLocator.getInstance() should have a synchronized block somewhere...shouldn't it?
Or , what exactly is the restriction of using synchronized block in our code blocks in ejb tier?

regards,
Sajid
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic