12.11.2014 Views

web server - Borland Technical Publications

web server - Borland Technical Publications

web server - Borland Technical Publications

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

In this element you specify the following:<br />

■<br />

■<br />

res-ref-name: this is the logical name for the resource, the same logical name you<br />

use in the element of the standard ejb-jar.xml descriptor file. This is<br />

the name your application components use to look up the datasource.<br />

jndi-name: this is the JNDI name of the datasource that will be bound to its logical<br />

name. It must match the value of the corresponding element of the<br />

element deployed with the DAR.<br />

■<br />

cmp-resource: this is an optional boolean element that is relevant to entity beans<br />

only. If set to True, the container's CMP engine will monitor this datasource.<br />

Let's look at an example entity bean that uses the Oracle datasource we defined<br />

above:<br />

<br />

entity_bean<br />

...<br />

<br />

jdbc/MyDataSource<br />

datasources/Oracle<br />

True<br />

<br />

...<br />

<br />

As you can see, we used the identical JNDI name from the <br />

element from the datasource definition. Now let's see how we obtain a datasource<br />

object reference. To do so, the application performs a lookup of the <br />

value of the deployed components and the object references are retrieved from the<br />

remote CosNaming provider. For example:<br />

javax.sql.DataSource ds1;<br />

try {<br />

javax.naming.Context ctx = (javax.naming.Context) new<br />

javax.naming.InitialContext();<br />

ds1 = (DataSource)ctx.lookup("java:comp/env/jdbc/MyDataSource");<br />

}<br />

catch (javax.naming.NamingException exp) {<br />

exp.printStackTrace();<br />

}<br />

A database java.sql.Connection can now be obtained from ds1.<br />

208 BES Developer’s Guide

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

Saved successfully!

Ooh no, something went wrong!