NotGaeL
NotGaeL

Reputation: 8484

Tomcat JSF Session BEan @Predestroy "Session already invalidated"

So I try to access an attribute of my HttpSession on my @PreDestroy method on a @SessionScoped JSF managed bean using

session.getAttribute("myAttribute"); 

But I get a

java.lang.IllegalStateException: getAttribute: Session has already been invalidated

Why?

I need to access the list of connections to external services opened by that session before one of my session beans is destroyed, and they are of course stored on a session attribute object.

How can I do that?

Upvotes: 1

Views: 1498

Answers (1)

BalusC
BalusC

Reputation: 1109272

Explicitly accessing a session attribute in a session scoped managed bean doesn't make sense. Just make that attribute a property of the session scoped managed bean itself.

@SessionScoped
public class YourSessionScopedBean implements Serializable {

    private Object yourAttribute; // It becomes a session attribute already.

    @PreDestroy
    public void destroy() {
        // Just access yourAttribute directly, no need to do it the hard way.
    }

}

The exception you faced occurred because the session was explicitly invalidated via a HttpSession#invalidate() call instead of "just" expired.

Upvotes: 4

Related Questions