2

I have two questions:

  1. Is it legal to access the RequestContext from PrimeFaces inside an @ApplicationScoped bean?
  2. Must the access to the RequestContext be synchronized?

Backing bean

@ApplicationScoped
public class MyDatabaseConnection
{
  public void do()
  {
    RequestContext ctx = RequestContext.getCurrentInstance();
    // ...
  }
}

index.xhtml

<p:commandButton actionListener="#{myDatabaseConnection.do}"/>
skuntsel
  • 11,624
  • 11
  • 44
  • 67
felipegf
  • 56
  • 5

1 Answers1

4

Is it legal to access the RequestContext from PrimeFaces inside an @ApplicationScoped bean?

Yes, as long as you don't assign it as an instance variable (property) of the application scoped bean.

Note that the same holds true for FacesContext, ExternalContext, HttpServletRequest, HttpServletResponse, HttpSesison, etc..etc.. They are all not application scoped and can thus cause major trouble when being assigned/shared as a property of an application scoped bean.

I must admit that your chosen bean class name MyDatabaseConnection really scares me in combination with @ApplicationScoped annotation. A database connection absolutely isn't intented to be application scoped. See also among others: Is it safe to use a static java.sql.Connection instance in a multithreaded system?


Must the access to the RequestContext be synchronized?

No, you don't need to. It's in the current code example been obtained in the method local scope and not assigned as a property of the bean.

The following answers should be helpful in understanding the meaning of "request", "session" and "application" scopes:

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