2

We have several JSF Managed beans with Request, View, and Session Scope and are running WebLogic 11g (10.3.2). Weblogic does not support the @EJB annotation in a JSF Managed Bean, so we have used these procedures http://technology.amis.nl/2008/12/06/ejb-dependency-injection-of-session-bean-facade-in-jsf-12-on-weblogic-103-jsf-with-jpa/ to create a ServletConextListener to load EJB references using the @EJB annotation.

Effectively, from within the JSF Managed Bean, we are then able to look up the EJB interface for the EJB that we wish to use by getting it from the ServletContext.

So the questions are:

1) Is it OK to make an EJB interface an instance variable on a ManagedBean? (the rationale is that the EJB is called many times during a page cycle)

2) if we do make them instance variables, should we mark the EJB Interfaces instance variables as transient?

BestPractices
  • 12,738
  • 29
  • 96
  • 140

1 Answers1

6

1) Is it OK to make an EJB interface an instance variable on a ManagedBean? (the rationale is that the EJB is called many times during a page cycle)

That's the normal design, yes. It's not different from when using @EJB. The returned EJB instance is a proxy anyway. The proxy will in turn worry about delegating the method calls to the proper and available concrete EJB instance in the EJB pool of the container.

Your only concern may be @Stateless versus @Stateful as seen relative to the JSF managed bean scope. You need to really understand what each the EJB session stands for. The @Stateless may return you a random instance on every call. The @Stateful gives you the same instance as long as the client (in this particular case, the JSF managed bean instance) lives. A more in depth explanation can be found here: JSF request scoped bean keeps recreating new Stateful session beans on every request?


2) if we do make them instance variables, should we mark the EJB Interfaces instance variables as transient?

Not needed. The EJB proxy is by default already serializable.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I ended up getting a NotSerializableException on my EJB that is an instance variable on my ViewScoped Managed Bean. Note I am not using @ EJB in my ManagedBean, but rather am using @ EJB in a ServletContextListener and then grabbing the EJB From the ServletContext (I am using WebLogic 10.3 that does not support @ EJB on a Managed Bean). Marking the EJB as transient solved this issue. – BestPractices Mar 26 '12 at 15:32
  • 2
    Marking it as transient would make you to lose it whenever the server serializes the session. This is absolutely not "best practice". Weblogic is a pain anyway. – BalusC Mar 26 '12 at 15:33