The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » GlassFish

Thread: Stateful EJB injection to the servlet

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 4 - Last Post: Feb 27, 2006 11:34 PM by: defectus
defectus

Posts: 21
Stateful EJB injection to the servlet
Posted: Feb 26, 2006 3:20 AM
  Click to reply to this thread Reply

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 ?

ksak

Posts: 455
Re: Stateful EJB injection to the servlet
Posted: Feb 27, 2006 7:01 AM   in response to: defectus
  Click to reply to this thread Reply

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

defectus

Posts: 21
Re: Stateful EJB injection to the servlet
Posted: Feb 27, 2006 7:44 AM   in response to: ksak
  Click to reply to this thread Reply

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.

ss141213

Posts: 499
Re: Stateful EJB injection to the servlet
Posted: Feb 27, 2006 8:56 AM   in response to: defectus
  Click to reply to this thread Reply

There is a similar issue about using @PersistenceContext discussed here:
http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html

Sahoo

defectus

Posts: 21
Re: Stateful EJB injection to the servlet
Posted: Feb 27, 2006 11:34 PM   in response to: ss141213
  Click to reply to this thread Reply

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 :-)




 XML java.net RSS