title: Instrumentation: Casting org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper to oracle.jdbc.OracleConnection
body: <p>I'm trying to instrument my jdbc connections. I know there are several similar questions about this topic. </p>
  
  <p>I tried everything but couldn't find the propper way to solve my issue so far.
  Also tried the answers to this question, with no result:
  <a href="https://stackoverflow.com/questions/6489514/apache-commons-dbcp-connection-object-problem-thread-classcastexception-in-org">Apache Commons DBCP connection object problem, Thread: ClassCastException in org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper</a></p>
  
  <p>I'm working with Tomcat 7 and Java 7. Here's where I define the oracle connection pool in my context.xml:</p>
  
  <pre><code>&lt;Resource name="jdbc/myDS" 
          type="javax.sql.DataSource"
          auth="Container"
          maxActive="350"
          maxIdle="50" 
          minIdle="10" 
          maxWait="10000" 
          username="user_own"
          password="mypassw"
          accessToUnderlyingConnectionAllowed="true"   
          driverClassName="oracle.jdbc.driver.OracleDriver"
          url="jdbc:oracle:thin:@192.168.110.173:1521/orcl" /&gt;
  </code></pre>
  
  <p>My instrumentation code:</p>
  
  <pre><code>private static void initInstrumentation(Connection con, final String usuario, final String modulo, final String accion) throws Exception {
  
      if (Utils.getParameter("instrumentation.active").equals("1")) {
  
          try {
  
              OracleConnection oracleConnection = null;
              //This is where I try to get the oracle connection, but no succeed
              if (con != null) {
  
                  if (con instanceof OracleConnection) {//NEVER COME IN HERE
                      oracleConnection = (OracleConnection) con;
                  } else if (con.isWrapperFor(OracleConnection.class)) {//NEVER COME IN HERE
                      oracleConnection = con.unwrap(OracleConnection.class);
                  } else{
                      //NO ORACLECONNECTION NO isWrapperFor -&gt; ALWAYS ENDS HERE!!!   
                      //oracleConnection = (OracleConnection)  ((DelegatingConnection) con).getDelegate();
                      oracleConnection = (OracleConnection) new DelegatingConnection(con).getInnermostDelegate();
                  }
              }
  
              if (oracleConnection != null) {
                  String[] metrics = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
                  metrics[OracleConnection.END_TO_END_MODULE_INDEX] = modulo;
                  metrics[OracleConnection.END_TO_END_ACTION_INDEX] = "Inicio: " + accion;
                  metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = usuario;
  
                  oracleConnection.setEndToEndMetrics(metrics, (short) 0);
              }
  
          } catch (Exception e) {
              throw new Exception("Error initInstrumentation " + e);
          }
      }
  }
  </code></pre>
  
  <p>My open connection method:</p>
  
  <pre><code>private static Connection genericOpenConnection() throws Exception {
      Connection con = null;
      try {
  
          DataSource dataSource = (DataSource) new InitialContext().lookup(Utils.cStrPlx(Utils.getParameter("dataSourceJndiName")));
          con = dataSource.getConnection();
          con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  
      } catch (Exception e) {
          Error.escribeLog("Error : " + e.getMessage());
          throw new SQLException(e.getMessage());
      }
      return con;
  }
  </code></pre>
  
  <p>So after calling openConnection and initInstrumentation I'm getting the next exception trying to cast the connection to an oracle connection. Any ideas of how to do this? What am I getting wrong?
  Thanks in advance.</p>
  
  <blockquote>
    <p>java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection</p>
  </blockquote>