Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lifetime of a stateful session bean

 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

i am just starting with EJB3 and have a (little) problem with a stateful session bean running under Glassfish (V2 UR1).

Here is the Bean Class:


The bean is called from a servlet

and subsequent calls to the servlet from my local Firefox give
Hello World (1)
Hello World (2)
...

So fine until here.

When i switch to Internet Explorer and call the servlet (which should start a new session, as i understand), the counter does not start again, but continues.

Same behaviour when the HttpSession expires (verified with a session listener). The next call to the servlet creates a new HttpSession, but the counter does not restart, but continues.

Did i really totally missunderstand stateful session beans, or should there be a new stateful session bean, when a new HttpSession starts?

Regards,
Uli
[ April 15, 2008: Message edited by: Uli Hofstoetter ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uli,
Welcome to JavaRanch!

Is the bean an instance variable in the servlet? If so, the servlet still has a reference to the first browser's associated session bean. And as multiple users share a servlet, the second browser could see that.

The stateless session bean itself should work as you describe if called properly.
 
Uli Hofstoetter
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeanne

and thanks for the welcome.

You exactly described the "problem", which is not really a problem, but the behaviour.

I found out by now that dependency injection with servlets and stateful session bean does not work due to the multithreaded nature of servlets.

To use a stateful session bean in a servlet, the bean instance has to be looked up via JNDI and then stored in the HttpSession.

Regards,
Uli
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic