7

I am new to ejbs and I want to know whether a stateful session bean will be destroyed or not. I injected a stateful session bean in my servlet. Even after 30min I see that the bean is active. I am using jboss eap 6.0. I believe that HttpSession has nothing to do with a stateful session bean. I invalidated the HttpSession to make sure that HttpSession has nothing to do with the statful session bean. So what if my application has many users and for each user if I create a new bean, the performance of my server is brought down. How does the container manage stateful session beans. When is a bean removed or destroyed?

I read this post and few others. But I did not get clarity.

Community
  • 1
  • 1
Krishna Chaitanya
  • 2,199
  • 3
  • 31
  • 56
  • 1
    Check out the Oracle [Java EE 7 tutorial](http://docs.oracle.com/javaee/7/tutorial/doc/partentbeans.htm#BNBLR). They have an example for a statefull session bean. If you want to build a scalable application, stay away from statelfull server code and rather have the client maintain all state. – Ralf Jan 30 '14 at 18:05
  • Thank you, that helped. `When the client terminates, its session bean appears to terminate and is no longer associated with the client.` I closed and opened my browser and sent the same request and I can see that the stateful bean is no more. – Krishna Chaitanya Jan 31 '14 at 06:54
  • That still doesn't answer for me exactly how it works though. I know that it is the client that keeps a stateful bean alive, but how does that work in relation to a browser? I assume that you made the EJB session scoped? – Gimby Jan 31 '14 at 07:47
  • Yeah, the ejb is session scoped. I think the app server will maintain somekind of timestamps, browser details and something else to determine the session. – Krishna Chaitanya Jan 31 '14 at 08:28

2 Answers2

2

A Stateful Session Bean is not client contextualized and must be destroyed explicitly within your code.

1) The SFSB must define a method annotated by @Remove :

@Stateful
@Local(ILocalQuiz.class)
public class QuizBean implements ILocalQuiz{

//...

    @Remove
    @Override
    public void end() {
        System.out.println("QuizBean instance will be removed..");
    }

}

2) The SessionScoped bean must call explicitly end() method :

public void cleanUp(){
    System.out.println("Cleaning up before destroying the SessionScoped  bean.");
    quizProxy.end();
}
1

@SessionScoped annotation only makes sense in a web context, outside a web context you should assume that your @SessionScoped will be ignored and your stateful EJB will behave like an old regular stateful ejb, and you shouldn't inject a stateful resource (old regular stateful EJB) into a stateless one (Servlet), in that case the scope of your stateful ejbs will depend on the scope of the instances of your servlet, and the java servlet spec. doesn't strictly require the container to create a servlet instance per session or request, as a matter of fact, some containers will use a single instance of your servlet to serve all clients, in which case you'd end up having this ugly situation where a single stateful ejb instance would be serving all your clients, check this out,

Stateful session beans unexpected behaviour when packaged in a war and packaged in an ear->jar

Community
  • 1
  • 1
Camilo
  • 1,819
  • 2
  • 15
  • 16