Connection Wrapping
Wrapping a Java class is a common object-oriented programming technique,
which consists of creating entirely new classes that implement the same interfaces
as the original classes. In general, the wrapped classes forward to the
original classes, but special codes can be added to the former. J2EE containers
use this technique to intercept JDBC calls and intersperse transaction management,
exception handling, connection management, performance
enhancements, and logging services on top of the original conection object.
Since Oracle 9i Release 2, the Oracle JDBC drivers support connection wrapping
through the oracle.jdbc.OracleConnectionWrapper class, which
implements java.sql.Connection and OracleConnection.
Obtaining a Physical Connection from a Wrapped Connection
The DataSource implementation in the Application Server provides a
getConnection method that retrieves the JDBC driver’s
SQLConnection from the Application Server’s
Connection wrapper. The method signature is as follows:
public java.sql.Connection getConnection(java.sql.Connection con)
throws java.sql.SQLException
For example get Unwrapped Connection from Datasource:
InitialContext ctx = new InitialContext();
com.sun.appserv.jdbc.DataSource ds =
(com.sun.appserv.jdbc.DataSource)
ctx.lookup("jdbc/MyBase");
Connection con = ds.getConnection();
Connection drivercon = ds.getConnection(con);
// Do db operations.
con.close();
Working Demo for Wrapped & Unwrapped Connection
For Oracle:-
package com.ram.triadic.db;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleConnectionWrapper;
import oracle.jdbc.pool.OracleDataSource;
public class WrappedConnection {
public static void main(String args[]) throws SQLException {
/** Getting UnWrapped Connection */
OracleDataSource ods = new OracleDataSource();
String url = "jdbc:oracle:thin:@:1521:";
ods.setURL(url);
ods.setUser("XXX");
ods.setPassword("XXX");
OracleConnection conn = (OracleConnection) ods.getConnection();
/**Wrap the connection */
System.out.println(" **** Wrapped Connection ***");
OracleConnectionWrapper wrappedConn =
new OracleConnectionWrapper(conn);
Statement stmt = wrappedConn.createStatement();
ResultSet rset = stmt.executeQuery("select * from tab");
while (rset.next())
System.out.print(rset.getString(1) + " ");
rset.close();
stmt.close();
/**Wrap the connection End*/
System.out.println();
/** unwrapping the wrapped Connection */
System.out.println(" **** UnWrapped Connection ***");
OracleConnection orig = ((OracleConnection)
wrappedConn).unwrap();
stmt = orig.createStatement();
rset = stmt.executeQuery("select * from tab");
while (rset.next())
System.out.print(rset.getString(1) + " ");
rset.close();
stmt.close();
System.out.println();
conn.close();
}
}
Note:-
Include ojdbc14.jar to your Class path
If you get a Connection from an Application Server JDBC connection pool, create a
Statement object, and then use the
Statement.getConnection method, the statement returns the physical connection instead of the wrapped connection. When you close this physical connection, you break the connection pool logic. To avoid this problem, use the following
asadmin create-jvm-options command, then restart the server:
asadmin create-jvm-options -Dcom.sun.appserv.jdbc.wrapJdbcObjects=true |
Reff:- Obtaining a Physical Connection from a Wrapped Connection (Sun Java System Application Server Enterprise Edition 8.2 Developer's Guide)