Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stateful Session Bean Undestanding - Ivan's note

 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and what shows your console?
 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you open the URL in Chrome within 10 seconds (before the first Session bean gets destroyed)?
 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 2270
76
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thanks for pointing this out!
I will update the notes as soon as I am able to.
Best wishes,
Ivan
 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch Frits! thanks a lot.. it works as expected. thanks again :-)
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic