Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@EJB annotation vs JNDI lookup

Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB annotation?

We're using JSF 1.2 with Sun Application Server 9.0_01.

Our team is debating which approach is better when using SLSBs in a Managed Bean.

I've read the following questions, but was wondering if there was a situation where lookup is preferred.

  • EJB3 - obtaining bean via injection vs lookup - what are the differences, implications, gotchas?

  • @EJB injection vs lookup - performance issue

like image 285
jahroy Avatar asked Feb 19 '23 23:02

jahroy


2 Answers

Is there any situation where it's better to use JNDI than to inject a stateless session bean using the @EJB annotation?

There's no situation where it's better--but situations where it's necessary:

  • when the name to lookup is not known at compile time (I would argue that it's bad design, but that's another issue)
  • when annotations are not supported, e.g. in regular non-managed helper classes and few other cases (We could again argue about whether it's good or bad to depends on EJB in these classes).

If the name to look up is constant and injection is possible, prefer @EJB annotations:

  • Make testing easier
  • Less trouble figuring out local / global JNDI names
like image 151
ewernli Avatar answered Feb 23 '23 12:02

ewernli


JNDI lookup might be important in case of SFSB (be sure to access the same instance all the time), but in case of SLSB I am not aware of any cases where JNDI would be "better" in any way.

I'd definitely go with @EJB. It's easier to read (less error-prone code), easier to maintain (you don't care about JNDI namespace location of the bean) and easier to test (no nasty lookup code you need to cut off when performing unit tests).

When talking about performance reasons - I am not 100% sure but I wouldn't be surprised if it'd occur that the application server is in fact doing a JNDI lookup behind the scenes when you use annotations.

like image 31
Piotr Nowicki Avatar answered Feb 23 '23 13:02

Piotr Nowicki