0

The problem is the following: I need to serialize the user session, so, it will still be present after a server restarts.

Using JavaEE and Tomcat 7 works fine with implements Serializable, but the problem is the FacesContext. Indeed, after restarting the server, FacesContext.getCurrentInstance() returns null, and therefore I cannot access the message bundle (and therefore my message.properties cannot be found anymore).

So, how do I keep the FacesContext when restarting Tomcat?

Littm
  • 4,923
  • 4
  • 30
  • 38
s4m3
  • 39
  • 6
  • This question is maybe more clear: http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Frameworks/JSF/Q_24773002.html but i do not want to register there – s4m3 Sep 06 '12 at 15:31

1 Answers1

1

Your problem description is unclear, but the symptoms suggest that you're trying to get hold of the current instance of the FacesContext anywhere as an instance variable. You shouldn't do that at all, you should always get the current instance in the local method scope.

So, you should never do e.g.

private FacesContext context;

public Bean() {
    context = FacesContext.getCurrentInstance();
}

public void someMethod() {
    context...doSomething();
}

but you should instead do:

public void someMethod() {
    FacesContext.getCurrentInstance()...doSomething();
}

Otherwise you're in case of a session scoped bean only holding the instance of the very first HTTP request which created the bean. Exactly this HTTP request is garbaged by end of the associated response and not valid anymore in any subsequent requests. The FacesContext is thus definitely not supposed to be serializable.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi @BalusC. I inject the `FacesContext` via `@Inject` annotation to the `@SessionScoped` bean. After restarting the `Tomee`, it's null. Is there a method to tell the `Tomee` to reinitialize transient attributes or should i get the `FacesContext` manually or do something else? – Arash Sep 19 '22 at 09:42