Search code examples
javajakarta-eeejb

resource injection in ejb


I don't understand the difference between lookup parameter and name parameter of the @Resource annotation. I read all the documentation about the annotation and its field etc.

I know how name can be used to inject or bind a resource, but the problem is that did not find any example that explain the difference between lookup and name.

Can anyone give me a such example or tell me the difference between them ?


Solution

  • name is a "virtual" reference name in the java:comp/env namespace. At some level, that reference needs to point to a "physical" resource that has been defined. The standard way to do that is with the lookup attribute, but products will also have a way to "bind" the the resource reference, so in many products, the product-specific binding is functionally equivalent to the name. (The product-specific binding is still important because you don't want to rely on developers to hard-code data source names from the operational environment, etc.)

    For example, you might have @Reference(name="jdbc/myDS", lookup="jdbc/oracleDS"), which means java:comp/env/myDS will be defined as an indirect lookup to the jdbc/oracleDS data source that was bound to the default JNDI namespace.

    For another example, you might have @DataSourceDefinition(name="derbyDS") and @Resource(name="jdbc/myDS", lookup="java:comp/env/derbyDS"). The former defines an actual data source (again relative to java:comp), and the latter defines an indirect lookup to it.