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.
- preferred: remove the jar file that supplies DelegatingConnection from your war file and rely on it being supplied by the common/lib classloader.
- run your code in the common/lib classloader by placing the war there
- reflect : probably bad, probably will give trouble casting the result object to the right class