title: Connection cannot be cast to oracle.jdbc.OracleConnection
body: <p>Why java.sql.Connection cannot be cast to oracle.jdbc.OracleConnection in code below?</p>
  
  <p>My main goal is to pass to Oracle connection new user name and save it in 'SESSION' table in for example 'osuser' column because I want to trace in DB user changes and display it in the table.</p>
  
  <pre><code>@Repository
  public class AuditLogDAOImpl implements AuditLogDAO {
  
      @PersistenceContext(unitName="myUnitName")
      EntityManager em;
  
      @Resource(name = "dataSource")
      DataSource dataSource;
  
      public void init() {
  
          try {
              Connection connection = DataSourceUtils.getConnection(dataSource);
              OracleConnection oracleConnection = (OracleConnection) connection; //Here I got cast exception!
  
              String metrics[] = new String[OracleConnection.END_TO_END_STATE_INDEX_MAX];
              metrics[OracleConnection.END_TO_END_CLIENTID_INDEX] = "my_new_username";
  
              oracleConnection.setEndToEndMetrics(metrics, (short) 0);
  
              java.util.Properties props = new java.util.Properties();
              props.put("osuser", "newValue");
  
              oracleConnection.setClientInfo(props);
  
          } catch (SQLException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Here is error log:</p>
  
  <pre><code>10:42:29,251 INFO  [STDOUT] org.jboss.resource.adapter.jdbc.jdk6.WrappedConnectionJDK6@bcc8cb
  10:42:51,701 ERROR [STDERR] java.lang.ClassCastException: $Proxy286 cannot be cast to oracle.jdbc.OracleConnection
  </code></pre>
  
  <p>Generally I have 2 problem in this case:</p>
  
  <ul>
  <li>why cast from Connection to OracleConnection fails and</li>
  <li>what is the best way to implement my intend (I mean set the new user name to v$session.osuser in Oracle DB?</li>
  </ul>
  
  <p>I work with Oracle 11g, Hibernate (using entity manager), data source via jndi.</p>
  
  <p>Please help, thanks!</p>
  
  <h2>EDIT:</h2>
  
  <p>After some improvement the problem with casting still exists.</p>
  
  <p>Improvement:</p>
  
  <pre><code>Connection connection = DataSourceUtils.getConnection(dataSource);
  connection = ((org.jboss.resource.adapter.jdbc.WrappedConnection)connection).getUnderlyingConnection();
  OracleConnection oracleConnection = (OracleConnection) connection;
  </code></pre>
  
  <p>Error:</p>
  
  <pre><code>java.lang.ClassCastException: $Proxy287 cannot be cast to org.jboss.resource.adapter.jdbc.WrappedConnection
  </code></pre>