glassfish
  1. glassfish
  2. GLASSFISH-16824

Stateful EJB state not preserved with keepstate=true

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.1
    • Fix Version/s: None
    • Component/s: ejb_container
    • Labels:
      None

      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.

        Activity

        Hide
        marina vatkina added a comment -

        Mahesh, please take a look.

        Show
        marina vatkina added a comment - Mahesh, please take a look.
        Hide
        Mahesh Kannan added a comment -

        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)

        Show
        Mahesh Kannan added a comment - 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)
        Hide
        Mahesh Kannan added a comment -

        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

        Show
        Mahesh Kannan added a comment - 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
        Hide
        Mahesh Kannan added a comment -

        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

        Show
        Mahesh Kannan added a comment - 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

          People

          • Assignee:
            Mahesh Kannan
            Reporter:
            arungupta
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: