简体   繁体   中英

org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection

connection file

InitialContext initialContext = new InitialContext();
Context context = (Context) initialContext.lookup("java:comp/env");

//The JDBC Data source that we just created
DataSource ds = (DataSource) context.lookup("connpool");
this.con = ds.getConnection();
this.des=ArrayDescriptor.createDescriptor("ARRAY_INT",this.con);

System.out.println("in set array1");
this.arr_to_pass=new ARRAY(this.des,this.con,arr);
this.csmt.setArray(index, this.arr_to_pass);

but i got the following exception

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection

I guess ClassCastException is thrown at this line

this.con = ds.getConnection();

Your code doesn't show the type of this.con , but it must be an OracleConnection . You can't do that, because connection is a wrapped dbcp connection.

If you want to take Oracle Connection, you have to unwrap it before with BasicDataSource.unwrap(Class<T> iface) . You can also use BasicDataSource.isWrapperFor(Class<?> iface) before, to check if the wrapped connection is of Oracle Connection type, to avoid casting exception :)

For example:

if (ds.getConnection().isWrapperFor(OracleConnection.class)) {
    this.con = ds.getConnection().unwrap(OracleConnection.class);
}

If you get below error ,make sure you have got connection with database

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper cannot be cast to oracle.jdbc.OracleConnection at

If there is no connection then you may get this error.

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