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.
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.
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.
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.
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.