• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Apache Commons DBCP connection object problem

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

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.

CallableStatement c_stmt = conn.prepareCall("begin update_message_ids_ota
(:x); end;" );
c_stmt.setArray( 1, array_to_pass );
c_stmt.execute();



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 ]
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the solution ranchers,

Here it is:
((DelegatingConnection)conn).getInnermostDelegate(); will give you the underlying Connection Object. ;)
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harvinder,
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:

Connection conn = jdbcTemplate.getDataSource().getConnection();
OracleConnection ocon = ((DelegatingConnection) conn).getInnermostDelegate();

In abve cas i got null reference.

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

Please Help me out.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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.




Avadhut Joshi
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somebody please reply asap.
I used
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bumping an old thread...
The answer to this question can be found at http://stackoverflow.com/a/6489566
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic