• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Stateful Session Bean Undestanding - Ivan's note

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and what shows your console?
 
Shankar sanjay
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3412
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?.
 
Ranch Hand
Posts: 138
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :
https://coderanch.com/t/611980/java-EJB-SCBCD/certification/Comparing-stateful-session-bean-stateless
 
Happily living in the valley of the dried frogs with a few tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic