1
votes

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?

1
Accessing session attributes in a session scoped JSF managed bean? Why not store them into a true session scoped managed bean instead? - Tiny
what do you mean by "true"? - NotGaeL
"true" means "true". A managed bean designated with a true @SessionScoped annotation. - Tiny
Well it is a managed bean designated with a @SessionScoped annotation. What am I missing here? What do you mean by "true" @SessionScoped annotation? CDI? EJB? They are all part of the standard Java EE API. - NotGaeL
I think @Tiny means that you should have the one with the lists injected instead of manually loading it from the session. For that it needs to be a ManagedBean (JSF, CDI, whatever) too. I hope the PreDestroy is called then in the order of 'Depending' and then Dependent so you can access it - Kukeltje

1 Answers

4
votes

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.