1

I have 2 @SessionScoped beans in my small application. Even when I restart of clean the server, all of the form values are retained.

@ManagedBean(name = "bill")
@SessionScoped
public class Bill implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static final String MANAGED_BEAN_NAME = "bill";

/**
     * @return the current instance of Bill
     */
    public static Bill getCurrentInstance() {
        return (Bill) FacesContext.getCurrentInstance().getExternalContext()
                .getSessionMap().get(MANAGED_BEAN_NAME);
    }
//other methods
}

@ManagedBean
@SessionScoped
public class TailorTip implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Bill bill;
private Participants[] participants;
private Integer participantIndex;

@PostConstruct
public void init() {
    bill = Bill.getCurrentInstance();
}

//other methods
}

How do I avoid the retention of values? Note: I use tomcat.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rajath
  • 2,178
  • 6
  • 32
  • 38

2 Answers2

3

This is indeed fully expected. Many servers save and restore sessions on restart, so that the endusers can continue with their website sessions. This is under the covers achieved by Java serialization. That's also exactly the reason why session scoped beans needs to implement Serializable.

As to the solution, there are several ways to fix this:

  1. Put the bean in the right scope for the job. Are you absolutely sure that they need to be session scoped? Isn't the view scope a better fit for those beans, after all? The session scope is usually only used for client-specific information, such as logged-in user, its preferences, etc, and thus not for form data. Your apparent urgent need to clear them on every server restart confirms that the session scope is the wrong choice. See also How to choose the right bean scope?

  2. Get rid of Serializable interface declaration in the class. This way they'll fail to be revived on server restart.

  3. Tell Tomcat to not revive sessions on server restart. Edit context.xml to add a <Manager> element with an empty pathname to the <Context> element.

    <Context ... >
        <Manager pathname="" />
    </Context>
    

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

If they're session scoped, then many servers retain session information across restarts. If you redeploy the application, then the information will be cleared. But simply restarting the application will not reset it. You'd have to consult your servers documentation to disable session persistence across server restarts.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203