|
Replies:
4
-
Last Post:
Feb 27, 2006 11:34 PM
by: defectus
|
|
|
|
|
|
|
Stateful EJB injection to the servlet
Posted:
Feb 26, 2006 3:20 AM
|
|
|
Hello, I'm little bit confused of @EJB injection of stateful session bean to the servlet. The problem is, that the bean is injected on the initilization of the servlet and lately this bean is shared across all connections (as there is only one instance of the servlet). This means, that number of users can share the same EJB in the same state. So, is there any possibility to instruct application server to inject an EJB on-demand ?
|
|
|
|
|
|
|
Re: Stateful EJB injection to the servlet
Posted:
Feb 27, 2006 7:01 AM
in response to: defectus
|
|
|
Hi, Injection is always tied to a class instance(except in the case of Java EE Application Clients), so the @EJB field for the Stateful Session bean in the servlet instance will always be only a single bean reference. You're correct in pointing out the the injection of resources that don't support concurrent access within a servlet instance is error-prone.
One recommended approach is to declare the @EJB at the type(Class) level, look it up via java:comp/env and store it in each HttpSession.
You would declare it as follows :
@EJB(name="myejbref", beanInterface=Foo.class) public class MyServlet ...
Note that there is no injection going on here since the annotation appears at the class level. This is an alternative to declaring the ejb dependency via an ejb-local-ref/ejb-ref in web.xml.
Next, you would look up the dependency via java:comp/env :
InitialContext ic = new InitialContext(); Foo foo = (Foo) ic.lookup("java:comp/env/myejbref");
If Foo is a remote or local business interface of a stateful session bean, each lookup will result in a reference to a NEW stateful session bean. You would then store it in the HttpSession instead of the servlet instance itself.
--ken
|
|
|
|
|
|
|
|
Re: Stateful EJB injection to the servlet
Posted:
Feb 27, 2006 7:44 AM
in response to: ksak
|
|
|
Thanks for reply ! I'll try it at home lately tonight. And to make it clear - this is a question from audience on our community meeting.
|
|
|
|
|
|
|
|
Re: Stateful EJB injection to the servlet
Posted:
Feb 27, 2006 11:34 PM
in response to: ss141213
|
|
|
Excellent - thank you guys a lot ! Sahoo, I'm reading your blog regulary but somehow I have overlooked this particular post, so take my apologize
|
|
|
|
|