web server - Borland Technical Publications

web server - Borland Technical Publications web server - Borland Technical Publications

techpubs.borland.com
from techpubs.borland.com More from this publisher
12.11.2014 Views

Connecting to JDBC Resources from J2EE Application Components Armed with this information, let's complete our datasource definition for the Oracle datasource we started above. In order to be thorough, let's first reproduce the XML we started above: datasources/Oracle datasources/OracleDriver False connectionType Enumerated Direct ... Note the driver datasource JNDI name in bold. Now we'll add the following: datasources/OracleDriver oracle.jdbc.pool.OracleConnectionPoolDataSource user String MisterKittles password String Mittens ... // other properties as needed ... Now the JDBC datasource is fully defined. Once you've packaged the XML file as a DAR, you can deploy it to a Partition. Doing so registers the datasource with the Naming Service and makes it available for lookup. Connecting to JDBC Resources from J2EE Application Components In Borland proprietary deployment descriptors, such as ejb-borland.xml for EJB components, the element is used to map a datasource logical name to actual JNDI location of a JDBC datasource definition. Mapping of the logical name to its location occurs when a JNDI lookup is performed for a desired datasource in the application component. You use the element within your individual component definitions. For example, a for an entity bean must be found within the tags. Let's look at the DTD representation of the element of Borland deployment descriptors: Chapter 22: Using JDBC 207

Connecting to JDBC Resources from J2EE Application Components In this element you specify the following: ■ ■ res-ref-name: this is the logical name for the resource, the same logical name you use in the element of the standard ejb-jar.xml descriptor file. This is the name your application components use to look up the datasource. jndi-name: this is the JNDI name of the datasource that will be bound to its logical name. It must match the value of the corresponding element of the element deployed with the DAR. ■ cmp-resource: this is an optional boolean element that is relevant to entity beans only. If set to True, the container's CMP engine will monitor this datasource. Let's look at an example entity bean that uses the Oracle datasource we defined above: entity_bean ... jdbc/MyDataSource datasources/Oracle True ... As you can see, we used the identical JNDI name from the element from the datasource definition. Now let's see how we obtain a datasource object reference. To do so, the application performs a lookup of the value of the deployed components and the object references are retrieved from the remote CosNaming provider. For example: javax.sql.DataSource ds1; try { javax.naming.Context ctx = (javax.naming.Context) new javax.naming.InitialContext(); ds1 = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDataSource"); } catch (javax.naming.NamingException exp) { exp.printStackTrace(); } A database java.sql.Connection can now be obtained from ds1. 208 BES Developer’s Guide

Connecting to JDBC Resources from J2EE Application Components<br />

Armed with this information, let's complete our datasource definition for the Oracle<br />

datasource we started above. In order to be thorough, let's first reproduce the XML we<br />

started above:<br />

<br />

<br />

datasources/Oracle<br />

datasources/OracleDriver<br />

False<br />

<br />

connectionType<br />

Enumerated<br />

Direct<br />

<br />

<br />

...<br />

Note the driver datasource JNDI name in bold. Now we'll add the following:<br />

<br />

datasources/OracleDriver<br />

oracle.jdbc.pool.OracleConnectionPoolDataSource<br />

<br />

user<br />

String<br />

MisterKittles<br />

<br />

<br />

password<br />

String<br />

Mittens<br />

<br />

...<br />

// other properties as needed<br />

...<br />

<br />

<br />

Now the JDBC datasource is fully defined. Once you've packaged the XML file as a<br />

DAR, you can deploy it to a Partition. Doing so registers the datasource with the<br />

Naming Service and makes it available for lookup.<br />

Connecting to JDBC Resources from J2EE Application Components<br />

In <strong>Borland</strong> proprietary deployment descriptors, such as ejb-borland.xml for EJB<br />

components, the element is used to map a datasource logical name to<br />

actual JNDI location of a JDBC datasource definition. Mapping of the logical name to<br />

its location occurs when a JNDI lookup is performed for a desired datasource in the<br />

application component. You use the element within your individual component<br />

definitions. For example, a for an entity bean must be found within the<br />

tags. Let's look at the DTD representation of the element of<br />

<strong>Borland</strong> deployment descriptors:<br />

<br />

Chapter 22: Using JDBC 207

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!