SFSB not storing values?
tsar_bomba Mar 28, 2006 10:33 PMI'm not sure what is wrong...but I can't get a Stateful bean to store any values!
I did a test using the example in the TrailBlazer app and got a SFSB to work just fine...but now that I'm trying one in my application I simply can't get it to retain any values I add into it.
It's really pretty simple. I have a ShoppingCartBean that has a globally declared collection of OrderLine objects:
@Stateful
public class ShoppingCartBean
implements Serializable ,ShoppingCartLocal, ShoppingCartRemote
{
private float orderTotal = 0F;
private Order order = new Order();
private List<OrderLine> orderLines = new ArrayList<OrderLine>();
private Customer customer = new Customer();
@EJB private ProductsLocal products;
public ShoppingCartBean()
{
}
@PostConstruct
public void init()
{
System.out.println("ShoppingCartBean called, orderLines count: " + orderLines.size());
}
public Order getOrder()
{
return this.order;
}
public Customer getCustomer()
{
return this.customer;
}
public List<OrderLine> getOrderLines()
{
return this.orderLines;
}
public int getItemCount()
{
return this.orderLines.size();
}
public boolean containsItem(OrderLine item)
{
return orderLines.contains(item);
}
public void addOrderLine(OrderLine item)
{
this.orderLines.add(item);
System.out.println("addOrderLine called, orderLines count: " + orderLines.size());
}
public void removeOrderLine(OrderLine orderLine)
{
this.orderLines.remove(orderLine);
}
public float getOrderTotal()
{
return this.orderTotal;
}
@Remove
public void checkout()
{
System.out.println("To be implemented");
}
}
I created a test servlet where I add a value and then display the contents of the collection:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
response.getWriter().println("SFSBTest!<br />");
try
{
InitialContext ctx = new InitialContext();
ShoppingCart cart = (ShoppingCart)ctx.lookup("MyEAR/ShoppingCartBean/local");
OrderLine ol = new OrderLine();
ol.setQuantity(1);
//add to cart SFSB
cart.addOrderLine(ol);
List<OrderLine> lines = cart.getOrderLines();
for (OrderLine line : lines)
{
response.getWriter().println(line.getQuantity() + "<br />");
}
}
catch (NamingException exp)
{
exp.printStackTrace();
}
catch (IOException exp)
{
exp.printStackTrace();
}
}
...instead...I see only the one object I'm adding...nothing new gets appended. The List is losing its contents rather than retaining and appending a new value on every refresh.
You'll see that I put out.println statements in the "add" and "get" methods in the SFSB for the collection so I could see what was happening while it ran.
Here's the output I see while refreshing:
20:15:39,418 INFO [STDOUT] ShoppingCartBean called, orderLines count: 0 20:15:39,418 INFO [STDOUT] addOrderLine called, orderLines count: 1
Did I forget something? What's going on? Am I misunderstanding how Stateful beans work?
Thanks!