9/20/2006

Default JNDI Name for Resource or EJB Injection

When a resource or EJB reference is injected to a component class, not only can you directly use this variable, you can also lookup with JNDI. For example,

package demo;
public class FooServlet extends HttpServlet {
@Resource(name="jdbc/mysql-ds")
private DataSource dataSource;

protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
Connection conn = null;
try {
InitialContext ic = new InitialContext();
DataSource ds =
(DataSource) ic.lookup("java:comp/env/jdbc/mysql-ds");

//the following 2 statements work the same:
conn = dataSource.getConnection();
con = ds.getConnection();
} catch (NamingException ex) {
throw new ServletException(ex);
} catch (SQLException ex) {
throw new ServletException(ex);
} finally {
//close con conn here
}
}
}
If the name() field is not specified for @EJB or @Resource, the default jndi name is not the variable name; instead, it's in the form of fully-qualified-classname/variable-name. Therefore, using default jndi name, the above example needs to be rewrite as follows:
package demo;
public class FooServlet extends HttpServlet {
@Resource
private DataSource dataSource;

protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
Connection conn = null;
try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(
"java:comp/env/demo.FooServlet/dataSource");

//the following 2 statements work the same:
conn = dataSource.getConnection();
con = ds.getConnection();
} catch (NamingException ex) {
throw new ServletException(ex);
} catch (SQLException ex) {
throw new ServletException(ex);
} finally {
//close con conn here
}
}
}

5 comments:

siva said...

Hi

I have just started reading EJB3 specifiacation for my SCBCD exam and came to know about this very useful site.

I am facing an problem when running an application.The have explained my problem in the javaranch website http://www.coderanch.com/t/488677/EJB-Certification-SCBCD/certification/Resource-injection-env-entry-field

Can you please tell me what am i doing wrong here?

javahowto said...

Siva,

env-entry is slightly different than other resources. In EJB 3 you still need to declare it in a descriptor (ejb-jar.xml in your case).

Then in your code (EJB bean classes or their interceptors), you can either look it up in the traditional way, or inject it to a field or property with @Resource


count
java.lang.Integer
100


@Resource(name="count")
private int count;

Or better if in Java EE 6 / EJB 3.1 / GlassFish V3:

@Resource(lookup="java:comp/env/count")
private int count;

javahowto said...

<env-entry>
<env-entry-name>count</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>100</env-entry-value>
</env-entry>

This works w/ any compliant appserver. GlassFish is one of them.

siva said...

Thanks I got it now.

Steve Smith said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Course in Chennai

Java Online Training India