java.lang.ClassCastException: com.mchange.v2.c3p0.impl.NewProxyConnection

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

i am getting the following

java.lang.ClassCastException: com.mchange.v2.c3p0.impl.NewProxyConnection 

when the following code executes. could you please help me how to solve.

            ComboPooledDataSource connPool = new ComboPooledDataSource();         connPool .setJdbcUrl(PropertyReader.getSystemProperty(DB_URL));                  connPool .setUser(PropertyReader.getSystemProperty(DB_USER));                    connPool .setPassword(Decryption.getDecryptedPwd(DB_PASSWORD));         connPool .setMaxPoolSize(MAX_POOL_SIZE);         connPool .setMaxIdleTime(MAX_IDLE_TIME);         connPool .setMinPoolSize(MIN_POOL_SIZE);         connPool .setMaxAdministrativeTaskTime(15);          java.sql.Connection conn = connPool.getConnection();         oracle.sql.CLOB c = CLOB.createTemporary(conn, false, CLOB.DURATION_SESSION);         Writer writer = c.setCharacterStream(0L);         writer.write(String.valueOf(pNoListDelimited).toCharArray());                   writer.flush();                     writer.close(); 

Exception stack trace

            java.lang.ClassCastException: com.mchange.v2.c3p0.impl.NewProxyConnection             at oracle.sql.CLOB.createTemporary(CLOB.java:676)             at oracle.sql.CLOB.createTemporary(CLOB.java:640)             ……             …..             at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:269)             at java.util.concurrent.FutureTask.run(FutureTask.java:123)             at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:651)             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:676)             at java.lang.Thread.run(Thread.java:595) 

回答1:

you are using Oracle-specific API that expects your Connection to be a particular Oracle Connection class. But the Connection you are working with is a c3p0 proxy Connection, not that Oracle Connection class.

if you want to use the Oracle-specific api, you have a few choices:

  1. you can use c3p0's raw Connection operations, see http://www.mchange.com/projects/c3p0/index.html#raw_connection_ops for the precise example you are looking for;
  2. c3p0 actually ships with a jar file for Oracle that performs this operation. it's rarely used and ages old, though, so a grain of salt;
  3. you can upgrade to a prerelease version of c3p0-0.9.5, which supports the full JDBC4 api, and use the unwrap operation to extract the raw Oracle Connection.

good luck!



回答2:

Unwrap your c3p0 proxy connection

 java.sql.Connection conn = connPool.getConnection();  conn = conn.unwrap(OracleConnection.class); 

Make sure you are properly closing you connection.



回答3:

Easiest way:

  1. Add this dependency:

    <dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jdbc</artifactId>    <version>4.1.3.RELEASE</version> </dependency> 
  2. Create a C3P0NativeJdbcExtractor:

    public class C3P0NativeJdbcExtractorImpl extends C3P0NativeJdbcExtractor {      public Connection getNativeConnection(Connection con) throws SQLException {          return doGetNativeConnection(con);     }  } 
  3. Transform the original connection into nativeConnection:

    Connection nativeCon = new C3P0NativeJdbcExtractorImpl().getNativeConnection(ps.getConnection()); 
  4. Use nativeCon instead conn:

    oracle.sql.CLOB c = CLOB.createTemporary(nativeCon, false, CLOB.DURATION_SESSION); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!