Scenario: The user clicks "generate list", the system displays the list. Then, the user enters in an email address and clicks on "send list".
There are two client server requests, so to avoid reading the database twice i set up a EJB3 stateful bean. The problem is that i cannot get the bean to hold conversational state across multiple client server requests.
Code: Local interface
@Local
public interface IList{
public void setList(Collection<String> list);
public Collection<String> getList();
public void remove();
}
Code: Bean
@Stateful
public class List implements IList{
private Collection<String> list;
@Override
public void setList(Collection<String> list) {
this.list= list;
}
@Override
public Collection<String> getList() {
return this.list;
}
@Remove
@Override
public void remove(){
}
}
code: ejb-jar.xml: only the data encapsulated by the session tags for simplicity
<session>
<ejb-name>List</ejb-name>
<ejb-class>com.lists.List</ejb-class>
</session>
This is how i look up the bean and set the list
InitialContext ic = new InitialContext();
IList irc = (IList)ic.lookup("java:global/rdb/rdb.jar/List!com.lists.IList");
irc.setList(list);
On the next request, i look it up again and call getList()
InitialContext ic = new InitialContext();
IList irc = (IList)ic.lookup("java:global/rdb/rdb.jar/List!com.lists.IList");
list = irc.getList();
Note: I've changed the class names and method names and the look up name because i cannot show you the actual code.
No errors are logged. Debugging the code showed me that the data is not available.
I have another stateful bean on my system and it was coded exactly the same, and that works. Not sure why this isn't working.
Each time you perform programmatic JNDI lookup on a stateful EJB bean you recieve brand new instance of that EJB. It means that in your case it works as expected. What you need to do is to store a refence to your SFSB somewhere e.g. inside HttpSession object. Then on the subsequent request you get the same SFSB instance as you got on previous request.
It's very interesting. Are you sure they are coded exactly the same? Can you share a piece of code?