Using JDBC Data Sources with ADF Business Components

Written by Blaise Ribet, Oracle Corporation
May 2004

This tip explains how to use JDBC data sources with an ADF Business Components JSP application deployed on Oracle Application Server 10g. JDBC data sources provide a standard method for creating JDBC connections. They offer some advantages over JDeveloper connection objects: connection information is not hard-coded in any of your application files, and can be modified after deployment. This is very useful if you want to use a test database for development and testing, and a production database after the application has been deployed.

Sample Application

This tip uses an example ADF Business Components/Struts/JSP application. The example application was built with the default web application template, has a Struts page flow diagram with a single data page, and a single JSP page that displays Departments and Employees from the hr common schema. The name of the business components application module is HrModule. The business components were built using a JDeveloper connection called hr_conn; when the instructions refer to the hr_conn connection, you should substitute the name of your own connection.

The tip follows the steps for deploying a business components application as a web module; a section near the end of the document describes how to work with data sources if you are deploying your application as an EJB session bean.


Configuring your Application to use a Data Source

Find the data source location

JDeveloper automatically creates an OC4J data source for each database connection in your JDeveloper installation. To find the location of the datasource that has been created for the hr_conn connection:

  1. In the JDeveloper main menu, select Tools | Embedded OC4J Preferences.
  2. In the Embedded OC4J Preferences dialog, expand the to Data Sources | jdev-connection-hr_conn node. jdev-connection-hr_conn is the name of the data source that JDeveloper has created for the hr_conn connection.
  3. Select the JNDI Names tab.
  4. The location is in the Location: field. In this example, the location is jdbc/hr_connCoreDS.

Create a New Application Module Configuration

Next, create a new application module configuration that uses a data source instead of a JDeveloper connection (alternatively you could edit your existing application module configuration).

  1. In the application navigator, right-click your application module and select Configurations... from the context menu.
  2. In the Configuration Manager, click Copy to copy your default configuration, HrModuleLocal.
  3. Select the new configuration and click Edit.
  4. In the Business Component Configuration dialog, change the configuration's name to HrModuleWithDS. Select JDBC DataSource as the connection type and enter jdbc/hr_connCoreDS as the datasource name, then click OK. The screenshot below shows the dialog with the new values entered:

Modify the Client to use the new Configuration

Next, modify your application client to use the new configuration you have just created.

  1. In the navigator, expand your ViewController project and select the DataBindings.cpx node (under Application Sources).
  2. In the structure pane, select your application module's data control (HrModuleDataControl in this example).
  3. In the property inspector, change the configuration to HrModuleWithDS, as shown in the screenshot below.
  4. Test your application: in the Struts page flow diagram, right-click the data page and select Run from the context menu.

Deploying your Application to the Application Server

Create a New Application Server Connection

This section describes how to deploy your application to Oracle Application Server 10g, Oracle 9i Application Server or standalone OC4J.

Note: Before deploying your application to the application server, you may need to install the ADF runtime libraries. See the document Application Servers Supported by JDeveloper.

  1. In the Connection Navigator, right click the Application Server node and select New Application Server Connection... from the context menu.
  2. Step through the connection wizard to create a new connection to your application server. Click Help for more information.

Create a Deployment Profile and Deploy Your Application

  1. In the application navigator, expand your ViewController project. Right-click web.xml (under Web Content) and select Create WAR Deployment Profile... from the context menu.
  2. In the Create Deployment Profile dialog, enter a deployment profile name (for example, deptEmpApp) and click OK.
  3. In the Deployment Profile Properties dialog, accept the defaults and click OK.
  4. In the application navigator, right-click webapp1.deploy and select Deploy to | connection_name in the context menu, where connection_name is the name of your application server connection.

    If you are deploying your application to Oracle Application server (9.0.3 or 10g), the steps listed above will deploy the application the the OC4J home instance. You can alternatively deploy your application to an EAR file, then use enterprise manager to deploy it to the application server, allowing you to deploy to a different instance of OC4J. See the OC4J and Enterprise Manager documentation for more information.

Modifying Data Sources on the Application Server

Finally, you modify the data source of your deployed application to connect to your production database. Note that your application server administrator or DBA can do this task without opening JDeveloper, and your developers do not need to be told the username and password of your production database.

Modify the Data Source on Oracle Application Server 10g or Oracle 9iAS

Follow these steps if you have deployed your application to Oracle Application Server 10g or Oracle 9iAS

  1. In a browser, connect to Enterprise Manager on your application server. You will need to know the enterprise manager port (usually 1810), username and password.
  2. In the Enterprise Manager home page, select the home link to go to the page for the OC4J home instance.
  3. On the OC4J: home page, select the Applications tab.
  4. In the table of applications, find the link for the application you have just deployed. This should be the same as the name of your deployment profile; deptEmpApp in this example. The screenshot below shows the OC4J home page, with deptEmpApp listed in the table of deployed applications.

  5. Select the link for your application (deptEmpApp in this example).
  6. On the application page, select the Data Sources link near the bottom of the page. The screenshot below shows the application page with the Resources link highlighted.

  7. On the data sources page, the table of data sources should contain a row with the name of your data source (jdev-connection-hr_conn) and its JNDI location (jdbc/hr_connCoreDS). Select this data source and click Edit. The screenshot below shows the table of data sources with the jdev-connection-hr_conn and the Edit button highlighted.
  8. In the Edit Data Source page, change these fields to the appropriate values for your production database:
    • JDBC URL
    • Username
    • Password
    Note: Indirect passwords provide an additional level of security. The online help for this page gives more information about indirect passwords, including links to application server documentation. Click Help at the top or bottom of the page to read the online help.
  9. Click Apply to save your changes.
  10. The confirmation page will then prompt you to restart the OC4J server. Click Yes.

Modify the Data Source on Standalone OC4J

If you have deployed your application to a standalone OC4J server you can modify a data source by editing the OC4J data sources file (data-sources.xml) directly.

  1. Edit the file OC4J_HOME\applications\ your_app_name\META-INF\data-sources.xml, where OC4J_HOME is the location of your OC4J home directory (for example, c:\oc4j\j2ee\home) and your_app_name is the name of your application (for example, deptEmpApp).
  2. Find the entry for your data source and modify the username, password, and url parameters as needed.
  3. Restart the OC4J server.

Test your Application

When the OC4J server has restarted, you can test your application. A quick way to find the URL to access your deployed application is to run the local copy of your application in JDeveloper, and copy the target URL from JDeveloper's Embedded OC4J Server message window. Paste this URL into your browser, and replace the embedded OC4J hostname and port with your production application server's hostname and port.

Working with JDeveloper 9.0.3 and 9.0.4

If you are developing your application in JDeveloper 9.0.3 or 9.0.4, there are some differences in the steps you have to follow to work with data sources, mainly because earlier versions of JDeveloper do not have a dialog for editing data sources or property inspector support for editing the client data model (cpx) file. This section describes these differences, working with a BC4J JSP application.

Configuring your Application to Use a Data Source

Find the data source location

  1. In your file system, find your workspace folder. By default this is jdev_home\jdev\mywork\ workspace_name, for example c:\jdev904\jdev\mywork\Workspace1.
  2. Open the file workspace_name-data-sources.xml, for example Workspace1-data-sources.xml.
    Note: this file is not created until you have run your JSP application in the embedded OC4J server in JDeveloper.
  3. Find the location entry for your database connection; this is the value you will use in your new BC4J configuration. The screenshot below shows the entry for the data source jdev-connection-hr_conn (this is the data source that JDeveloper created for the hr_conn connection); the location is jdbc/hr_connCoreDS.

Create a new application module configuration

The steps for creating a new application module are the same in JDeveloper 9.0.3, 9.0.4 and 10g (9.0.5).

Modify the client to use the new configuration

  1. Your client project contains a file with the .cpx suffix, for example Project2.cpx. Find the location of this file, then close down JDeveloper (this file can only be edited outside of JDeveloper).
  2. Locate the .cpx file in the file system, edit it and change the Configuration entry to the name of your new configuration.
  3. Save your changes and restart JDeveloper.

Deploying your Application to the Application Server

Follow the usual steps, described in the online documentation, for deploying your application to Oracle Application Server or Standalone OC4J.

Modifying Data Sources on the Application Server

Follow the steps described earlier in this document.

Working with Business Components Deployed as an EJB Session Bean

When you create a deployment profile for deploying your business components as an EJB session bean, JDeveloper automatically creates a new business components configuration that uses the correct data source, you should use this configuration and not create a new configuration yourself. You do need to modify your client to use the new configuration, then follow the steps to deploy your business components as described in the JDeveloper online documentation

Links to Additional Information

false ,,,,,,,,,,,,,,,