Binding to a data source

 

During either application assembly or deployment, bind the resource reference to the actual name of the resource in the run time environment. One can take this action in the AAT or as one of the steps during installation of the application EAR file.

 

Bean-managed persistence bean

When developing your BMP bean you generally lack knowledge about the name of the data source on the target appserver. In your code, do not look up the data source directly. Instead, you look up the resource reference from the java:comp/env namespace file. Let us assume that you look up the resource reference named ref/ds as illustrated in the code below.

javax.sql.DataSource dSource = 
(
    javax.sql.DataSource
)
(
    (new InitialContext()).lookup("java:/comp/env/ref/ds")
)
(
    javax.sql.DataSource
)
javax.rmi.PortableRemoteObject.narrow
( 
    (new IntialContext()).lookup("jdbc/Section"), 
    javax.sql.DataSource.class
); 

In the AAT, you specify the name ref/ds in the Resource Reference page on the General Tab. If you know the name of the data source you can specify it in this Resource References page on the Bindings Tab. Note that if you do not specify it here , provide this JNDI name when you install the application EAR file.

 

Container-managed persistence bean

In a CMP bean you do not specify the DataSource in the code. Instead you specify the JNDI name of the DataSource during assembly using the AAT, this is the setting in the Bean Binding panel. In this example, the JNDI name jdbc/Section is used.

 

Servlets and JSP Files

In a servlet application, you look up the DataSource exactly as you look it up in the BMP bean case.


Connection factory
Data sources
Unshareable and shareable connections
Creating and configuring a JDBC provider and data source using the JMX API
Configure data access for application clients
Creating or changing a resource reference
Assembling data access applications
Creating and configuring a JDBC provider and data source