DBA Tips Archive for Oracle


Data Sources and OC4J

by Jeff Hunter, Sr. Database Administrator


Applications deployed to the OC4J application server (and even those that exist outside of the application server) can use a data source object to connect to a database. This article provides an overview on how to configure and test a data source in the OC4J application server. I will then create a simple Java client application that exists outside of the OC4J application server to connect to an Oracle database.

Data Source Overview in OC4J

A data source is a Java object that has the attributes and methods specified in the javax.sql.DataSource interface. It is a factory for java.sql.Connection objects. In almost all cases, an object that implements the DataSource interface will register itself with a JNDI service.

Configuring Data Sources in OC4J

Configuring data sources in OC4J is done through the file data-sources.xml. This file is located in $OC4J_HOME/j2ee/home/config directory. Keep in mind that this does not uses or configure any connection pooling. Add the following entry to your data-sources.xml file. Change the host:port:SID in the url to your own connection information:

NOTE: After modifying the data-sources.xml file, you must restart the OC4J application server.

NOTE: This article provides the details on how to configure a data source using the standalone version of OC4J. If you are using Oracle9iAS, you should use Enterprise Manager(EM) to create and configure data sources.

Example Database Client Java Program

In this section, I will present a complete Java client program that can be used to lookup an OC4J data source. This is an application that will exist outside of the OC4J application server (an application not deployed to the OC4J application server) that will use JNDI to lookup an OC4J data source, acquire a Connection object, connect to and query from an Oracle database.

Setting the CLASSPATH

set CLASSPATH=.;C:\JDeveloper\j2ee\home\oc4j.jar

Compile the Java Client Program

javac DataSourceClient.java

Run the Program

java DataSourceClient
Initial Context  : javax.naming.InitialContext@64dc11
Data Source      : com.evermind.sql.OrionCMTDataSource@10655dd
Connection       : [Connection : com.evermind.sql.OrionCMTConnection@1d7ad1c]

User             :  SCOTT
Date / Time      :  22-MAY-2004 16:02:55

// -----------------------------------------------------------------------------
// DataSourceClient.java
// -----------------------------------------------------------------------------

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import java.util.Hashtable;

 * This is a simple Java client that can be used to lookup a J2EE data source
 * in OC4J. Once the data source is acquired, it will be used to get a
 * Connection object to connect to a database.
 * @author Jeffrey Hunter, Sr. Database Administrator
 * @author <a href="mailto:jhunter@iDevelopment.info">jhunter@iDevelopment.info</a>
 * @author <a target="_blank" href="http://www.iDevelopment.info">www.iDevelopment.info</a>
 * @version 1.0,    17-MAY-2004
 * @since SDK1.4
public class DataSourceClient  {

    public static void main(String[] args) {

        Statement stmt        = null;
        ResultSet rset        = null;
        String    queryString = null;

        try {
            // STEP 1: Create a Hashtable object for JNDI Connection
            Hashtable env = new Hashtable();

            // STEP 2: Configure all JNDI properties
                , "com.evermind.server.rmi.RMIInitialContextFactory");

            env.put(Context.SECURITY_PRINCIPAL, "admin");
            env.put(Context.SECURITY_CREDENTIALS, "manager");
            env.put(Context.PROVIDER_URL, "ormi://localhost:23791");

            // STEP 3: Create the context object using the JNDI env object
            Context ctx = new InitialContext(env);
            System.out.println("Initial Context  : " + ctx);

            // STEP 4: Look up the OC4J data source object
            DataSource ds = (DataSource)ctx.lookup("jdbc/OracleTestDS");
            System.out.println("Data Source      : " + ds);

            // STEP 5: Now, acquire a Connection object and connect to the
            //         Oracle database.
            Connection conn = ds.getConnection();
            System.out.println("Connection       : " + conn + "\n");

            // STEP 6: Use the Connection object to query from the database.

            queryString =
                "SELECT " +
                "    user " +
                "  , TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS') " +
                "FROM dual";
            stmt = conn.createStatement();
            rset = stmt.executeQuery(queryString);

            int counter = 0;
            while (rset.next()) {
                System.out.println("User             :  " + rset.getString(1));
                System.out.println("Date / Time      :  " + rset.getString(2));


        } catch (SQLException e) {
            System.out.println("SQL Exception");
        } catch (NamingException e) {
            System.out.println("Naming Exception");


Emulated and Non-Emulated Data Sources

Emulated datasource - The pre-installed default data source is an emulated data source. Emulated data sources are wrappers around Oracle data sources. Used primarily by applications that access only a single database.

Non-emulated datasource - Non-emulated data sources are pure Oracle data sources. Used by applications that want to coordinate access to multiple sessions within the same database or to multiple databases within a global transaction.

Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Saturday, 01-Feb-2003 00:00:00 EST
Page Count: 28202