@EJB annotation versus JNDI search

Is there a situation where it is better to use JNDI than to insert a session without bean state using the @EJB annotation?

We are using JSF 1.2 with Sun Application Server 9.0_01.

Our team discusses which approach is best when using SLSB in a managed Bean.

I read the following questions, but wondered if there was a situation where a search was preferable.

+4
source share
3 answers

Is there a situation where it is better to use JNDI than to embed a stand-alone bean session using the @EJB annotation?

There is no situation where it is better - but situations in which it is necessary:

  • when the name for the search is unknown at compile time (I would say that this is a bad design, but this is another problem)
  • when annotations are not supported, for example. in ordinary non-managed helper classes and in a few other cases (we could argue again about whether this depends on EJB in these classes for good or bad).

If the search name is persistent and injection is possible, prefer @EJB annotations:

  • Make testing easier
  • Less trouble finding out local / global JNDI names
+4
source

Finding a JNDI can be important in the case of SFSB (be sure to refer to the same instance all the time), but in the case of SLSB I am not aware of cases where JNDI will be "better" in any way.

I would definitely go with @EJB . It’s easier to read (less error prone code), easier to maintain (you care about the location of the JNDI namespace for the bean), and easier to test (without the nasty search code that needs to be disabled when performing unit tests).

Speaking of efficiency - I'm not 100% sure, but I won’t be surprised if it happens that the application server actually performs JNDI searches behind the scenes when using annotations.

+3
source

Is there a situation where it is better to use JNDI than to insert a session without bean state using the @EJB annotation?

This question and relative answer describes a specific situation that requires a JNDI SLSB search: Inheritance JPA Inheritance and EJB . Essentially, when the SLSB class name is determined at runtime.

+1
source

Source: https://habr.com/ru/post/1437293/


All Articles