Download
FAQ History |
API
Search Feedback |
Database Connections
The Application Server ships with a relational database product named PointBase. The following material shows how the
SavingsAccountBean
example of Chapter 26 accesses a PointBase database. TheSavingsAccountBean
component is an entity bean with bean-managed persistence.Session beans and Web components will use the same approach as
SavingsAccountBean
to access a database. (Entity beans with container-managed persistence are different. See Chapter 27.)Coding a Database Connection
For the
SavingsAccountBean
example, the code that connects to the database is in the entity bean implementation classSavingsAccountBean
. The source code for this class is in this directory:The bean connects to the database in three steps:
- Specify the logical name of the database.
private String dbName
= "java:comp/env/jdbc/SavingsAccountDB";The
java:comp/env
portion of the logical name is the environment naming context of the component. Thejdbc/SavingsAccountDB
string is the resource reference name (sometimes referred to as the coded name). Indeploytool
, you specify the resource reference name and then map it to the JNDI name of theDataSource
object.- Obtain the
DataSource
object associated with the logical name.
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);Given the logical name for the resource, the
lookup
method returns theDataSource
object that is bound to the JNDI name in the directory.- Get the
Connection
object from theDataSource
object.
Connection con = ds.getConnection();
Specifying a Resource Reference
The application for the
SavingAccountBean
example is in theSavingsAccountApp.ear
file, which is in this directory:For your convenience, the resource reference and JNDI names in
SavingsAccountApp.ear
have already been configured indeploytool
. However, you may find it instructive to openSavingsAccountApp.ear
indeploytool
and follow these steps for specifying the resource reference.
- In
deploytool
, selectSavingsAccountBean
from the tree.- Select the Resource Ref's tab.
- Click Add.
- In the Coded Name field, enter
jdbc/SavingsAccountDB
.- In the Type combo box, select
javax.sql.DataSource
.- In the Authentication combo box, select Container.
- If you want other enterprise beans to share the connections acquired from the
DataSource
, select the Sharable checkbox.- To map the resource reference to the data source, enter
jdbc/ejbTutorialDB
in the JNDI Name field.If the preceding steps are followed, the Resource Ref's tab will appear as shown in Figure 31-1.
Figure 31-1 Resource Ref's Tabbed Pane of
SavingsAccountBean
Creating a Data Source
In the preceding section, you map the resource reference to the JNDI name of the data source. The
deploytool
utility stores this mapping information in a deployment descriptor ofSavingsAccountBean
. In addition to setting the bean's deployment descriptor, you also must define the data source in the Application Server. You define a data source by using the Admin Console. To create the data source with the Admin Console, follow this procedure:
- Open the URL
http://localhost:4848/asadmin
in a browser.- Expand the JDBC node.
- Select the JDBC Resources node.
- Click New.
- Type
jdbc/ejbTutorialDB
in the JNDI Name field.- Choose
PointBasePool
from the Pool Name combo box.- Click OK.
- Note that
jdbc/ejbTutorialDB
is listed under the JDBC Resources node.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.