How to use DataSources in a Web Application on Oracle OAS10g

There are many how-to documents on using DataSources in web applications. I’m adding yet another, mainly because I want one in a location that I can remember when I need to point someone at it. Additionally, there aren’t a ton of documents on how to set all this up for OAS10g application server.

This document will outline the proper way to reference a DataSource on OAS and how to set up the application-specific configuration file with a default JNDI mapping for the logical DataSource.

We’ll go through the following steps in setting up our application:

  1. Choose a logical name for the DataSource
  2. Add a <resource-ref> element declaring the logical name for the DataSource in our application’s web.xml file
  3. Write some application code to use the DataSource
  4. Update OAS-specific deployment descriptor to map our logical DataSource name to a physical DataSource on the application server

First, we’ll need to make up a logical name for the DataSource that we wish to use from our application. We’ll use this name to referene the DataSource throughout our application. We choose a logical name so that we can map it to a physical DataSource.

We do this to avoid hard-coding a reference to a physical DataSource in our application. This is important because the physical environment tends to change over time. Also, our application may need to be deployed to a different environment. Creating a logical name for the DataSource allows us to configure our application to map to the appropriate physical DataSource at deployment time. As the physical environment changes, application server admins can re-map to a new DataSource as required.

Next, we need to formally declare our logical name for the DataSource. For this example, I’ve chosen “jdbc/my_ds” as the logical DataSource name. We’ll declare it by adding the following to our web.xml:

<!-- Map DataSources Used in App -->

  <description>Web Database DataSource</description>

Now that we’ve created a logical reference to a DataSource, we can refer to it in our application as: “java:comp/env/jdbc/my_ds”

We’ll use the following java code in our web application to get a reference to the DataSource:

private DataSource getDataSource() throws NamingException
  Context initialContext;
    initialContext = new InitialContext();
    Context envContext =
    DataSource ds =
    return ds;
  catch (NamingException e)
      // Do something useful with the error, log it, or whatever.
      // For the example, I'm simply going to rethrow it
      throw e;

You could go ahead and package and deploy your app at this point. If you use the Enterprise Manager application to deploy your application to OAS10g, you’ll have the opportunity to map your “jdbc/my_ds” logical name to a physical location. In my shop, we’re all about deploying from the command line so we can simply script our deploys. As we don’t use Enterprise Manager, we have to take an extra step to provide the mapping.

OAS10g uses a proprietary config file to set this up. You’ll need to add a file called orion-web.xml in your WEB-INF directory of the web application. For this example, I’ll map the logical “jdbc/my_ds” to the physical JNDI name on the application server “jdbc/com/javatastic/physical_ds_nontx”.

The DataSource mapping occurs in the following element:

<!-- Maps resource-ref names to physical resources -->
    location="jdbc/com/javatastic/physical_ds_nontx" />

Adding this to the orion-web.xml file allows us to configure a default mapping which will be honored by OAS10g even though we will do a scripted deploy. If we deploy using the Enterprise Manager tool, we’ll have the opportunity to override these settings.

Leave a Reply

Your email address will not be published. Required fields are marked *