简体   繁体   中英

throwing exception while using innermost class of org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper

conn= Ds.getConnection(); Connection oraconn = ((DelegatingConnection)conn).getInnermostDelegate();

its giving following exception java.lang.ClassCastException: org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to org.apache.commons.dbcp.DelegatingConnection

I have also set resource parameter accessToUnderlyingConnectionAllowed=true need to unwrapp this object to pass to ArrayDescriptor in server.xml of tomcat please help thanx in advance

It might be that your org.apache.commons.dbcp.DelegatingConnection is a different class (different classloader) than the org.apache.commons.dbcp.DelegatingConnection from the connection.

this can be, if you are supplying this class via your container-bound warfile, and the connection is instantiated via tomcat (or other application server).

to test this you can use something like:

Class.forName("org.apache.commons.dbcp.DelegatingConnection").getClassLoader().toString()
conn.getClassLoader().toString();

you might be able ot use reflection. this from a similar question :

// 'source' is from another classloader
Method method = conn.getClass().getMethod("getInnermostDelegate", new Class[] {});
Object o = method.invoke(conn, new Object[] {});

However, i'm not sure the return argument is easily castable to a class in your current classloader. If you follow the reflection method, make absolutely sure you encapsulate all of this in a single class, don't let things like Method and Object bleed out from this. I don't recommend this but it ought to work.

an alternative is to place your code in the same place as the connection. (tomcat lib)

ofcourse first check if indeed you are using 2 different classloaders for your code. :D

EDIT:

From the comments it looks like there are 2 classloaders at work here. I forgot to mention a 3rd approach.

  1. preferred: remove the jar file that supplies DelegatingConnection from your war file and rely on it being supplied by the common/lib classloader.
  2. run your code in the common/lib classloader by placing the war there
  3. reflect : probably bad, probably will give trouble casting the result object to the right class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM