Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
You can access a database through JDBC by creating an environment element for a JDBC DataSource
using deployment XML (see "Using Deployment XML").
Note:
In EJB 3.0, an environment reference to a resource manager connection factory is not needed. You can access a resource manager connection factory directly using resource injection (see "Looking Up an EJB 3.0 Resource Manager Connection Factory").For information on looking up a resource manager connection factory, see the following:
To define a reference to a JDBC DataSource
using deployment XML, do the following:
In the data-sources.xml
file, define the desired DataSource
and specify its actual JNDI name (see "Configuring Data Sources").
In this example, assume a DataSource
is specified in the data-sources.xml
file with the JNDI name of /test/OrderDataSource
.
Define a <resource-ref>
element in the appropriate client deployment descriptor (see "Where do you Configure an EJB Environment Reference?") and configure the following subelements, as Example 19-11 shows:
<res-ref-name>
: the logical name for the JDBC data source.
It is a best practice to prefix the reference name with jdbc
, but it is not required. If you use the initial context to look up this reference in your bean source code (see Example 19-30), always prefix the logical name with java:comp/env/
(for example, java:comp/env/jdbc/OrderDB
).
<res-type>
: the Java type of the resource. For the JDBC DataSource
object, this is javax.sq.DataSource
.
<res-auth>
: the source of authentication information, either Application
or Container
.
In the orion-ejb-jar.xml
deployment descriptor, define a <resource-ref-mapping>
and configure the following attributes, as Example 19-12 shows:
name
: the logical name of the data source (defined in ejb-jar.xml
).
location
: the actual name of the data source (defined in data-sources.xml
).
Example 19-12 Mapping Logical to Actual JDBC Data Source Resource Manager Connection Factory Using <resource-ref-mapping>
<resource-ref-mapping name="jdbc/OrderDB" location="test/OrderDataSource" />
Figure 19-6 shows a <resource-ref-mapping>
element with the name
attribute set to jdbc/OrderDB
(the logical name defined in ejb-jar.xml
) and the location
attribute set to test/OrderDataSource
(the JNDI name defined in data-sources.xml
).
Within the bean's implementation, you can look up the JDBC data source resource manager connection factory for this data source using the logical name java:comp/env/jdbc/OrderDB
(see Example 19-30).