[GLASSFISH-16824] Stateful EJB state not preserved with keepstate=true Created: 08/Jun/11  Updated: 08/Nov/11  Resolved: 28/Jun/11

Status: Closed
Project: glassfish
Component/s: ejb_container
Affects Version/s: 3.1
Fix Version/s: None

Type: Bug Priority: Major
Reporter: arungupta Assignee: Mahesh Kannan
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Tags: 3_1_x-exclude

 Description   

For a vanilla Java EE 6 project with the following stateful EJB:

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package org.glassfish.samples;

import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;

/**
*

  • @author arungupta
    */
    @Stateful
    public class NewSessionBean implements Serializable {

private ArrayList items;

@PostConstruct
public void init()

{ items = new ArrayList(); }

public void addItem(String item)

{ items.add(item); }

public void removeItem(String item)

{ items.remove(item); }

public ArrayList getItems()

{ return items; }

}

Injected in a Servlet as:

@Inject NewSessionBean bean;

used as:

bean.addItem("1");
bean.addItem("2");
bean.addItem("3");
out.println(bean.getItems());

and deployed as:

~/tools/glassfish/3.1/glassfish3/glassfish/bin/asadmin deploy --force=true --keepstate=true dist/WebApplication44.war

The state is not preserved across multiple redeploys. Each redeploy shows the array list initialized to blank. Tried initializing it outside @PostConstruct as well.



 Comments   
Comment by marina vatkina [ 08/Jun/11 ]

Mahesh, please take a look.

Comment by Mahesh Kannan [ 15/Jun/11 ]

Two things:

1. I am not sure if @Inject and @EJB are equivalent (Marina can confirm this). The servlet should use @EJB to inject EJBs into it.

2. Second, the Stateful EJB seem to be just injected and never kept inside the servlet session. So, upon redployment, when the servlet is instantiated, the injected EJB is in fact a newly "looked up" EJB. So it will not retain the old states.

Try adding the Stateful EJB into the HTTP Session and things should work. ( will test this myself though soon)

Comment by Mahesh Kannan [ 28/Jun/11 ]

As mentioned in my comment, the ejb must be retrieved from the http session. Else every redeployment will result in a creation of a new ejb instance(through lookup)

I have already explained this to Ludo and Arun

Comment by Mahesh Kannan [ 03/Nov/11 ]

As mentioned in my comment, the ejb must be retrieved from the http session. Else every redeployment will result in a creation of a new ejb instance(through lookup)

I have already explained this to Ludo and Arun. Closing this issue. The bug was actually in the app. Because it injected a new EJB the old preserved state was not used at all

Generated at Sat Jul 23 16:05:38 UTC 2016 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.