Question

I am studying up on EJB 3 from the book EJB in Action and this book in chapter 5 discusses about environment naming context(ENC). It says this :

If you know how JNDI references worked in EJB 2, you’re familiar with the environment naming context (ENC). ENC allows portability of the application without having to depend on global JNDI names. Global JNDI names for resources differ between application server implementations, and ENC allows you to use a JNDI location that starts with java:comp/env/ instead of hard-coding the actual global JNDI name. EJB 3 essentially assumes that all JNDI names used in code are local references and automatically prepends names with the java:comp/env/ prefix.

I am not getting what is meant by global JNDI name? Why does it have to be different across app servers?

I am tagging the question as both EJB2 and EJB 3 since the quote references both the versions. Please feel free to edit if you think otherwise.

Was it helpful?

Solution

A global JNDI name uses a name in the default JNDI context. For example, on WebSphere Application Server, remote EJBs are bound by default to ejb/<app>/<module>/<bean>#<interface>. If you hardcode this lookup string in your application, then the application won't be portable to other application servers (which might use a different scheme for the default name) or even the same WebSphere Application Server if the target application or module name changes.

The solution in EE is for your application to declare an EJB reference, which will be in the java:comp/env context with a name that you choose. When a deployer installs your application on the server, they can bind the java:comp/env name to a global JNDI name that is appropriate for the environment. This same pattern is actually used by all EE resources (data sources, String/int/boolean configuration values, etc.), and when used properly, it is one of the strengths of the EE platform.

I can't tell what the excerpt above is trying to say without more context. Perhaps it's referring to the EJBContext.lookup method, which is effectively a shortcut of doing a lookup relative to java:comp/env.

As a final aside, I'll note that EJB 3.1 defines a standard location of java:global/<app>/<module>/<bean>!<interface> for EJB bindings. Regardless, it is still a best practice to use EJB references when doing lookups across applications (or even across modules if you're not in full control of the application that will contain the EJB client).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top