Resource Manager References

In addition to configuration parameters and EJB references, the EJB specification also provides a portable method to reference resource manager factories from the bean class. Like the other environment references, resource manager references are logical names that can be used to reference server resources. For instance, a resource manager reference might point to a JDBC DataSource. Instead of hard-coding the data source name and JDBC driver information, the bean code refers to a logical name. At deployment time, the EJB deployer maps the logical name to a concrete resource factory.

Declaring Resource Manager References

Like other environment references, resource manager references are defined in the EJB deployment descriptor. Resource references use the resource-ref tag, which contains a description of the reference, the logical name, the resource type, and an authorization setting. The resource type depends on the underlying resource factory that is being mapped. The authorization setting may be either Container or Application. When the Container setting is used, the appropriate security permissions should be set in the WebLogic Server before deploying the EJB. In this case, the bean code does not contain code to sign on or authenticate itself with the resource manager. When the authorization setting is Application, it is the bean code's responsibility to explicitly sign on to the resource manager. The container authorization setting is generally preferred because it defers security settings to deployment time. This enables the same bean code to be used in different environments where the security settings may not be identical. The following resource-ref example creates a logical name jdbc/DBPool that maps to a JDBC data source:

<resource-ref>
  <description>
      This DataSource is mapped to a connection where the Widget
      EJB  store inventory information.
  </description>
  <res-ref-name>jdbc/DBPool</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

Like EJB references, resource manager references also require an entry in the WebLogic deployment descriptor. The resource-description tag includes the corresponding res-ref-name of the resource-ref and maps it to a server-wide JNDI name of a resource. Table 8.2 shows th e name of the Resource Type for each Resource manager.

Table 8.2. Resource Manager Types
Resource Manager Resource Type
JDBC DataSource javax.sql.DataSource
JMS Connection Factories javax.jms.QueueConnectionFactory
JavaMail Session javax.mail.Session
URL Connection Factory java.net.URL

For example, this entry in the weblogic-ejb-jar.xml maps jdbc/DBPool to the name DBPool.

<resource-description>
  <res-ref-name>jdbc/DBPool</res-ref-name>
  <jndi-name>DBPool</jndi-name>
</resource-description>

The final step in using a JDBC resource manager reference is mapping the server-wide JNDI name to an actual resource. For JDBC, this resource is a JDBC connection pool. The following config.xml entry maps the JNDI name DBPool to the connection pool named DevelopmentPool.

<JDBCTxDataSource
  Name="DBPool"
  Targets="myserver"
  JNDIName="DBPool"
  PoolName="DevelopmentPool"
/>

Resource Reference Advantages

At first glance, a lot of mapping and deployment descriptor elements are used to create a resource manager reference. However, this approach makes it easy for the deployer to reconfigure the application without changing the actual implementation classes. For instance, the development team might code and test the EJB in a private development environment. In this case, the JNDI name is mapped to the DevelopmentPool connecting to a development database. When the bean is deployed in the production system, the deployer can simply remap the DBPool JNDI name to the production database connection pool. No changes are required in the bean code or even the EJB deployment descriptors.

Like other environment references, the bean writer looks up resource manager references in the JNDI tree under the java:/comp/env context. By convention, JDBC DataSources are mapped under the JDBC subcontext; JMS connection factories typically use the JMS subcontext; JavaMail connection factories use mail; and URL connection factories use URL. The bean code can reference the jdbc/DBPool reference with the following code:

Context ctx = new InitialContext();
DataSource dataSource = (DataSource)
  ctx.lookup("java:/comp/env/jdbc/DBPool");

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.103.122