Background

Resource referencing allows one to lookup DataSource or EJB home interfaces using their alias names instead of the actual JNDI names. An administrator is responsible for mapping the alias names used in the code to the actual JNDI names of the resources.

To use an alias name, an application needs to use the java:com/env name space. For example:

 javax.naming.InitialContext ctx = new javax.naming.InitialContext();
 DataSource ds = (DataSource) ctx.lookup("java:comp/env/DataSourceAlias");

Main advantages of the resource name mapping are:

  1. An administrator can map the alias name of a DataSource to an existing DataSource that is already defined in the system. This way, DataSources can be shared by many unrelated applications.
  2. Multiple different Enterprise Application Modules can contain the same EJB without causing a JNDI name collision. The administrator would assign a unique JNDI name to the EJB for each application and then map the name to the alias used in the application’s code.

Creating a DataSource Reference

Let us say that from our Servlet code we look up a DataSource by the alias name – DataSourceAlias. There exists in the system a DataSource with the JNDI name jdbc/MyDataSource. The following steps will show you how to map the alias name to the actual JNDI name.

In WSAD open the deployment descriptor editor for the Web project that contains your Servlet. To do this, double click on the WEB-INF/web.xml file of the Web project. Click on the References tab. From the top of the page click on the Resource tab.

Under the Resource References list click on the Add button. Enter the alias name used in the code – DataSourceAlias. Next specify the following properties:

Type: javax.sql.DataSource. Use the Browse button to pick the name.
Authentication: Application or Container. Depending on what you select here, the corresponding JAAS authentication of the data source will be used (if the caller does not supply a user ID and password with the getConnection call). It is more secure to choose Container authentication. In your actual data source configuration do not supply a JAAS authentication alias for Application. The Application JAAS alias is also used as default when a Java application looks up the resource using the global name space. Setting the Application JAAS alias is a huge security risk because any Java application can do a JNDI lookup using the global namespace. If you set just the Container alias, only the Java code running in one of the application servers (and using java:com/env namespace) can access the resource without supplying a user id and password. A stand alone Java application can not successfully use the java:comp/env name space because there is no one to map the name to the actual JNDI name.
Sharing: Sharable.
WebSphere Bindings: Enter the actual JNDI name of the DataSource. In our case it is jdbc/MyDataSource.

Save and close the deployment descriptor editor.

Test DataSource Reference

Add a new Servlet within the web module. Write the doGet method as follows:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {

	javax.sql.DataSource ds = null;
	java.sql.Connection con = null;
	java.io.PrintWriter out = resp.getWriter();

	resp.setContentType("text/html");

	try {
		out.println("Looking up DataSource<br>");
		javax.naming.InitialContext ctx = new javax.naming.InitialContext();
		ds = (javax.sql.DataSource)
		ctx.lookup("java:comp/env/DataSourceAlias");
		out.println("Getting connection<br>");
		con = ds.getConnection();
		con.close();
	} catch (Exception e) {
		e.printStackTrace(out);
	}
	out.println("Done<br>");
}

Run the Servlet.

Adding EJB Reference

Let us say that our Servlet looks up a Session EJB using the alias name SessionBeanAlias and the EJB has an actual JNDI name of ejb/com/webage/ejb/DataTestHome. An administrator will need to create a map between the two names.

Open the web.xml file editor. Click on the References tab and select the EJB tab from the top. (To add a reference to a local home interface, click on the EJB Local tab). Click on the Add button and enter the alias name – SessionBeanAlias. Next to the Link property click on Browse. System will show a list of all EJBs defined in the Enterprise Application the web module belongs to.

Select the EJB and click on OK. System will automatically fill in the home interface, remote interface, EJB type and and JNDI names fields. Save and close the deployment descriptor editor.

USING RESOURCE REFERENCING

Test EJB Referencing

Add the following lines to the doGet method of the test Servlet you have already created.

	out.println("Looking up EJB<br>");
	Object obj = ctx.lookup("java:comp/env/SessionBeanAlias");