File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Lifetime of a stateful session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Lifetime of a stateful session bean" Watch "Lifetime of a stateful session bean" New topic

Lifetime of a stateful session bean

Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57

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?

[ April 15, 2008: Message edited by: Uli Hofstoetter ]

SCEA5, Certified ScrumMaster
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32819

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Uli Hofstoetter
Ranch Hand

Joined: Nov 24, 2006
Posts: 57
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.

subject: Lifetime of a stateful session bean
It's not a secret anymore!