my dog learned polymorphism*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Stateful Session Bean Undestanding - Ivan's note Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Stateful Session Bean Undestanding - Ivan Watch "Stateful Session Bean Undestanding - Ivan New topic
Author

Stateful Session Bean Undestanding - Ivan's note

Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
Hi,

My understanding is Stateful session bean has to maintain state for each client. I got an example from Ivan's tutorial 2.Create a Stateful Session Bean (http://www.slideshare.net/krizsan/ocp-jbcd-6-study-notes) page # 22.

1. I opened the following URL in IE, (its first request, so new Stateful Session Bean has to create)
http://localhost:8080/StatefulSession1Web/test.do?name=Ivan

2. I opened the following URL using Chrome (its second request, so new StatefulSession bean has to create).

http://localhost:8080/StatefulSession1Web/test.do?name=second

But seems only one stateful session been has been created. Can anyone please clarify me?

I am not sure anybody has tried that.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

Hi Shankar,

But seems only one stateful session been has been created. Can anyone please clarify me?

What gives you the idea that there is only one created? Can you post the code of your Session bean?

Regards,
Frits
Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

and what shows your console?
Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
INFO: *** StatefulSession1Bean 1 created.
INFO: *** StatefulSession1Bean 1 remove.
INFO: *** StatefulSession1Bean 1 destroyed.

My question was..

1. I opened the following URL in IE, (its first request, so new Stateful Session Bean has to create)
http://localhost:8080/StatefulSession1Web/test.do?name=Ivan

2. I opened the following URL using Chrome (its second request, so new (2nd) StatefulSession bean has to create).

http://localhost:8080/StatefulSession1Web/test.do?name=second

Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

Did you open the URL in Chrome within 10 seconds (before the first Session bean gets destroyed)?
Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
Yes tried in 10 secs only. otherwise i would have got NosuchEJBException, Later i tried with 300 Seconds too :-), but output same
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1700
    
  25

Ok, I have looked at the setup in Ivan notes and I have noticed that there is a serious mistake in his example.

There is only one Servlet responding to all the requests, and in that Servlet the EJB is injected once (i.e. for every client the same Stateful Session Bean). There will never be a second bean.

The way to implement this example is to add a HTTPSessionListener and do a lookup for an Stateful EJB and store in in the Session.

Add this listener to your code:

and add this to your web.xml

and do this in your Servlet:


When you run the example now, you will see what you had expected:
INFO: *** StatefulSession1Bean 1 created.
INFO: sessionCreated

INFO: *** StatefulSession1Bean 2 created.
INFO: sessionCreated

INFO: *** StatefulSession1Bean 3 created.
INFO: sessionCreated

INFO: *** StatefulSession1Bean 1 destroyed.

INFO: *** StatefulSession1Bean 3 destroyed.
INFO: *** StatefulSession1Bean 2 destroyed.


Regards,
Frits
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Thanks for pointing this out!
I will update the notes as soon as I am able to.
Best wishes,
Ivan


My free books and tutorials: http://www.slideshare.net/krizsan
Shankar sanjay
Ranch Hand

Joined: Sep 14, 2009
Posts: 117
Good catch Frits! thanks a lot.. it works as expected. thanks again :-)
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Finally, a new version of the book is available.
The above mentioned example has been corrected, as well as a number of other mistakes and typing errors I made.
Best wishes!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

for every request a new thread will be generated that will run the service() method. ain't different threads will be treated as separate clients ? or will they be treated as request coming from the same client ?.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

As per my understanding, following scenarios will be treated equally by the server.
1) Different requests from same client to the same servlet or
2) Different requests from different clients to same servlet,

Servlet's service() method will be executed in threads for both these scenarios.

Whether a new bean gets created or not, depends on the location of look-up code. If look-up is done in the doGet() or doPost() method, then every look-up of stateful bean should return a new instance.

We had a long discussion about this on this link :
http://www.coderanch.com/t/611980/java-EJB-SCBCD/certification/Comparing-stateful-session-bean-stateless


Regards,
Prajakta
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stateful Session Bean Undestanding - Ivan's note