This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I am using Apache Commons DBCP (commons-dbcp-1.2.2.jar) Connection pool.
Once i obtain a connection from the pool it is wrapped in the class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.
My requirement is to pass an array of Strings to pl/sql stored procedure in Oracle.
Here is what i am doing in the following code snippet:
Connection conn = ConnectionManager.ds.getConnection(); //The above statement returns me an connection wrapped in the class //org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.
DelegatingConnection delegate_conn = new DelegatingConnection(conn); //Obtain an object of class DelegatingConnection to get a reference to the //actual underlying connection object using the getDelegate() method.
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor( "OTA_MSGIDS_STRING_ARRAY", delegate_conn.getDelegate() ); //Pass the actual underlying connection object to the above //createDescriptor() method because the wrapped connection object as //mentioned above is not accepted by the createDescriptor() method.
ARRAY array_to_pass = new ARRAY( descriptor, delegate_conn.getDelegate(), arrOta_Message_Ids_Offered ); //Pass the actual underlying connection object to the above //ARRAY() constructor because the wrapped connection object as //mentioned above is not accepted by the ARRAY() constructor.
On executing the above code, I get the following exception:
java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:91)
Obviously, the reason above is that even though i am sending the underlying connection object but the code in createDescriptor() method is testing the connection object instance against an OracleConnection which it is not.
How can i use the Connection object returned by the Apache Commons DBCP connection pool to be compatible with the Connection object expected by ArrayDescriptor.createDescriptor(String str1, Connection conn1); method?
Any help is deeply appreciated. [ December 08, 2008: Message edited by: Harvinder Thakur ]
Joined: Jun 10, 2008
Hey guys, anybody got an answer. Also i am using the apache commons dbcp library in a standalone application i.e. no application/web server [ December 08, 2008: Message edited by: Harvinder Thakur ]
Joined: Jun 10, 2008
Got the solution ranchers,
Here it is: ((DelegatingConnection)conn).getInnermostDelegate(); will give you the underlying Connection Object. ;)
You have mentined that you got solution for above problem.
I am using the same solution,but still i am getting that problem.
I am using JdbcTemplate of spring framework to get connection.
Following is what i am doing:
if try with getDelegate method i got the wraaper object so it is giving classcast exception.
java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection
I am new to using DBCP..I am using more than one datasources for the application and I want to obtain the connection object of particular datasource.. In this case do I need to write my ConnectionManager class or it is provided by DBCP. How do I handle this..please help.
Somebody please reply asap.
if(con instanceof tomcat.dbcp.dbcp.DelegationConnection ) before applying delegation connection to make oracle .jdbc.oracleconnection and it is returning fa;lse.
If I dont use this 'if' statement and directly use Delegation Connection statement,then an exception is thrown:cant convert tomcat.dbcp.dbcp.PoolableConnection$PoolableConnection Wrapper to tomcat.dbcp.dbcp.Delegation Connection.Please please somebody guide me asap.