EJB stateful vs stateless

Datetime:2016-08-23 01:57:13          Topic: EJB           Share

I have been studying Java EE and I posted a question on Stackoverflow to verify my understanding of “SessionBean”, here is the summary:

Stateless bean in client:

Java

public void work(){
    bean.work1();   // this uses instance 1 in App Server
    ...
    bean.work2();   // this can be instance 2 in App Server
}
public void work(){
    bean.work1();  // this uses instance 1 in App Server
    ...
    bean.work2();  // this can be instance 2 in App Server
}

Stateful bean in client:

Java

public void work(){
    bean.add_item(item); // this uses instance 1 in App Server
    ....
    bean.checkout();     // this uses instance 1 in App Server
}
public void work(){
    bean.add_item(item); // this uses instance 1 in App Server
    ....
    bean.checkout();    // this uses instance 1 in App Server
}
  • A stateless session bean, should not store a state in the bean instance, in order words, it’s class members should not be assigned by external values.
  • A stateful session bean, maintains it’s state until it’s “flushed”, a bean method that is annotated by “@Remove”




About List