Application Techniques

Using EJB Resource References

How to create a bean reference in the deployment descriptor and look it up at runtime.

About this technique



Enterprise JavaBean Techniques


You'll learn about:

Related reading

See the part on developing Enterprise JavaBeans in the Programmer's Guide

Specifying resource references in the deployment descriptor   Top of page

To use the JAR Designer to add a resource reference to the deployment descriptor, you add the reference entry and specify its properties as shown here:

The property definitions are as follows:

Property value



This is the name that will be put in the JNDI environment reference and looked up by anyone trying to get the named resource.

The EJB specification recommends that this be prefaced with jdbc/

For example:


The deployer can later map this reference to the appropriate database.

Resource Reference type

This must be javax.sql.DataSource.

This contains the Java type of the factory not of the resource.

Authorization type

Values are: container or application.

Specifying container indicates that the container will sign on to the resource manager in order to obtain the resource factory. SilverStream uses the same username/password bindings as those used to add the database to the server. You must specify container for CMP beans.

Specifying application means that the code in the EJB signs on to the resource manager programmatically.

A single DataSource can have multiple name/password bindings, by using the getConnection(username, password) method from the DataSource interface.


A textual value that you can use to describe to the deployer what values are appropriate for the resource.

Using the JAR Designer to map resources   Top of page

At deployment, you can map the resource reference to a SilverStream database connection pool. If you deploy using the JAR Designer, you specify the binding to one of the databases that is currently available to the server, using the Property Inspector as shown here:

Using SilverCmd to map the resources   Top of page

If you deploy the EJB JAR using the SilverCmd DeployEJB, you need to specify this mapping in the command's XML input file. The following snippet shows how you the node that can accomplish this:


Looking up the resource at runtime   Top of page

You can find a referenced resource name in the java:comp/env directory as shown here:

  DataSource ds; 
  InitialContext m_initialContext = new InitialContext();                  
  Context contextEnv = (Context) m_initialContext.lookup("java:comp/env/"); 
  ds = (DataSource) contextEnv.lookup("jdbc/MyDataSource"); 

Resource scoping   Top of page

The EJB specification states that the resource factory reference is scoped to the bean whose entry in the deployment descriptor contains the reference to the resource factory, thus, the resource factory cannot be accessed from other beans at runtime. Other beans can also define resource references using the same names without causing a a name conflict.

Copyright © 2000, SilverStream Software, Inc. All rights reserved.