Lifecycle of FacesContext?
Asked Answered
I

1

7

While going through the javadoc of FacesContext, I came across this sentence

The instance remains active until its release() method is called, after which no further references to this instance are allowed. While a FacesContext instance is active, it must not be referenced from any thread other than the one upon which the servlet container executing this web application utilizes for the processing of this request

Does this mean that FacesContext will never go for garbage collection, and the instance will be destroyed only when the current-webapplication stops (server is stopped)?

Is FacesContext following singleton pattern? In that case, how will it behave when multiple request and coming for rendering response simultaneously as it serves only one request per time?

Impressure answered 26/4, 2013 at 6:16 Comment(0)
E
12

Does this mean that FacesContext will never go for garbage collection, and the instance will be destroyed only when the current-webapplication stops (server is stopped)?

No, you read it wrongly. The FacesContext lives as long as a single HTTP request. It will (actually, "can" is a better word) not immediately be GC'ed if you incorrectly reference it anywhere in your own code beyond its scope. E.g. as a property of a session scoped managed bean which lives longer than a single HTTP request:

@ManagedBean
@SessionScoped
public class BadSessionBean {

    // Bad Example! Never do this! Not threadsafe and instance can't be GC'ed by end of request!
    private FacesContext context = FacesContext.getCurrentInstance();

}

If you aren't doing that anywhere in your code, and thus you're always obtaining the current instance in method local scope, then it will have the chance to be properly GC'ed.

@ManagedBean
@SessionScoped
public class GoodSessionBean {

    public void someMethod() {
        // OK! Declared in method local scope and thus threadsafe.
        FacesContext context = FacesContext.getCurrentInstance();
    }

}

Please note that this GC behavior is not specific to JSF/FacesContext, it's just specific to basic Java in general.


Is FacesContext following singleton pattern? In that case, how will it behave when multiple request and coming for rendering response simultaneously as it serves only one request per time?

No, it's definitely not a singleton. It's a ThreadLocal instance which is created by FacesServlet right after the service() method is entered and destroyed by the FacesServlet right before the service() method is left. Thus, there's only one instance per request (and thus not per application). Note that one HTTP request counts as one separate thread. There can be multiple threads (read: requests) and thus there can be multiple instances of the FacesContext during application's lifetime. Its main pattern is the facade pattern, but that's further not related to it being a ThreadLocal.

See also:

Emergent answered 26/4, 2013 at 12:27 Comment(8)
Note that one HTTP request counts as one separate thread. There can be multiple threads (read: requests) and thus there can be multiple instances of the FacesContext during application's lifetime..Thanks for going in depth..I was looking for this sentence..:)Impressure
So in that case , it is not recommended to get an instance of FacesContext in a session scoped bean? As by rule the instance of FacesContext should not be shared between 2 Requests?Impressure
You're welcome. This related answer is in that case likely also helpful/enlightenend to understand better how servlets work under the covers of JSF: #3106952Emergent
No, you read it wrong again. It's not recommended to assign it as a property of a session scoped bean. You must instead obtain it in method local scope. Any variables declared in method block are threadlocal (and thus thread safe). I extended the answer with another example.Emergent
Ok..ok..then it will be destroyed once we get out of the method..as there will be no reference..Impressure
Note that it's not the context instance itself which get destroyed, it's just the object reference which get destroyed. Now pieces comes together, I strongly recommend to take a JSF pause and take some time to learn the Java basics. GC and object references are also covered in a sane basic Java tutorial like Oracle's own one and the SCJP book.Emergent
I had bookmarked the link regarding your explanation on Servlets a long time ago..:)Impressure
Reading that answer, I had the impression that in a session scoped managed bean, I have different requests for each method. Is that right?Clea

© 2022 - 2024 — McMap. All rights reserved.