This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ...
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?
I wouldn't bother to make these classes thread safe. It is possible that more than one instance could get created, so what is the harm ? Other option is to implement lazy loading singleton where again no threading issues come into picture.