How to cast from org.jboss.jca.adapters.jdbc.jdk8.

2019-08-05 07:30发布

问题:

How to cast from org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8 to oracle.jdbc.OracleConnection in java 1.8. At present i am using like this and got the following exception.

java.lang.ClassCastException: org.jboss.jca.adapters.jdbc.jdk8.WrappedConnectionJDK8 cannot be cast to oracle.jdbc.OracleConnection

session = getHibernateSession();
conn = getConnection(session);
conn.setAutoCommit(false);
oracleConnection = conn.unwrap(OracleConnection.class);

回答1:

You cannot use Connection.unwrap() on WrappedConnectionJDK8, very unfortunately. You have to call WrappedConnection.getUnderlyingConnection() instead. See also this question. In your case:

OracleConnection oracleConnection = (OracleConnection) 
    ((WrappedConnectionJDK8) conn).getUnderlyingConnection();

Alternatively, if you cannot access the WrappedConnectionJDK8 type, just use reflection:

OracleConnection oracleConnection = (OracleConnection) 
    conn.getClass().getMethod("getUnderlyingConnection").invoke(conn);

I know...



标签: java oracle jdbc