JDBC Connections in Oracle9iAS Containers for J2EE

Date: 22-Jul-2002

Java Database Connectivity (JDBC) provides a standard way for Java applications to access relational databases. This article shows different ways to connect to the database in different types of applications.

Acquiring a Connection in a Java application

This tip assumes Oracle thin driver to connect to the database. The library classes12.zip needs to be in the classpath, if we wish to connect to the database. The following snippet registers the driver and gets a connection to the database, in the case of a normal java application.
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
  Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@<hostname>:<port>:<sid>","<username>","<password>"); 

Acquiring a Connection in a J2EE application running on OC4J

This section tells how to get a connection within a Web application Servlet or JSP) on OC4J. A Java connection can be obtained if there is an entry available in the file datasources.xml, which is available in <OC4J_HOME>/config directory.
<data-source
            class="oracle.jdbc.pool.OracleConnectionPoolDataSource"

            name="ORCLDS"
            location="jdbc/OrclDS"
            xa-location="jdbc/xa/OrclXADS"
            ejb-location="jdbc/PooledOrclDS"
            url="jdbc:oracle:thin:@localhost:1521:orcl"
            connection-driver="oracle.jdbc.driver.OracleDriver"
            username="scott"

            password="tiger"
            inactivity-timeout="30"
   /> 
Assuming the above entry, the connection can be obtained as follows.
 javax.naming.InitialContext ic  = new javax.naming.InitialContext();
 javax.sql.DataSource dataSource = (javax.sql.DataSource)ic.lookup("jdbc/OrclDS");
 java.sql.Connection conection   = dataSource.getConnection();

Acquiring a vanilla OracleConnection

For getting a simple vanilla connection, the following entry needs to be made in the datasources.xml file.

 <data-source
            class="oracle.jdbc.pool.OracleDataSource"
            name="jdbc/pool/OracleDS"
            location="jdbc/pool/OracleDS"
            url="jdbc:oracle:thin:@localhost:1521:orcl"
            username="scott"
            password="tiger"

 /> 
The data source created in this case is of type oracle.jdbc.pool.OracleDataSource and the connection created when getConnection() is called is of type oracle.jdbc.driver.OracleConnection. The following Java code does the lookup:
 javax.naming.InitialContext ic               = new javax.naming.InitialContext();
 oracle.jdbc.pool.OracleDataSource dataSource = (oracle.jdbc.pool.OracleDataSource)ic.lookup("jdbc/pool/OracleDS");
 oracle.jdbc.driver.OracleConnection conn     = dataSource.getConnection(); 

Acquiring Pooled Connections

For getting a pooled connections, the following entry needs to be made in the datasources.xml file.
 <data-source

            class="oracle.jdbc.pool.OracleConnectionPoolDataSource"
            name="jdbc/pool/OracleConnectionPoolDS"
            location="jdbc/pool/OracleConnectionPoolDS"
            url="jdbc:oracle:thin:@localhost:1521:orcl"
            username="scott"
            password="tiger"
  /> 
The data source created in this case is of type OracleConnectionPoolDataSource. A getPooledConnection() call in the object returned from the lookup will return oracle.jdbc.pool.OraclePooledConnection and a getConnection() call will return oracle.jdbc.driver.OracleConnection. The following Java code does the lookup:
  javax.naming.InitialContext ic                     = new javax.naming.InitialContext();
  oracle.jdbc.pool.OracleConnectionPoolDataSource ds = oracle.jdbc.pool.OracleConnectionPoolDataSource)ic.lookup("jdbc/pool/OracleConnectionPoolDS");
  oracle.jdbc.pool.OraclePooledConnection pc         = ds.getPooledConnection();
  oracle.jdbc.driver.OracleConnection conn           =  pc.getConnection(); 

Acquiring XA Connections

For getting XA connections, the following entry needs to be made in the datasources.xml file.
  <data-source
            class="oracle.jdbc.xa.client.OracleXADataSource"
            name="jdbc/xa/OracleXADS"
            location="jdbc/xa/OracleXADS"
            url="jdbc:oracle:thin:@localhost:1521:orcl"

            username="scott"
            password="tiger"
  /> 
The data source created in this case is of type oracle.jdbc.xa.client.OracleXADataSource. A getXAConnection() call on this data source will return oracle.xa.client.OracleXAConnection.
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy