简体   繁体   English

如何将Jdbc4Connection转换为PGConnection?

[英]How to cast Jdbc4Connection to PGConnection?

I want to make use of postgres CopyManager like: 我想利用postgres CopyManager

CopyManager cp = ((PGConnection) dataSource.getConnection()).getCopyAPI();

As I'm using spring-boot , the datasource is a org.apache.tomcat.jdbc.pool.DataSource , thus the connection a Jdbc4Connection . 因为我使用的是spring-boot ,所以datasource是一个org.apache.tomcat.jdbc.pool.DataSource ,因此连接一个Jdbc4Connection

Problem: The casting throws the following error: 问题:转换会抛出以下错误:

java.lang.ClassCastException: com.sun.proxy.$Proxy55 cannot be cast to org.postgresql.PGConnection

Also, when I try to cast to a Jdbc4Connection, I get the very same error! 此外,当我尝试强制转换为Jdbc4Connection时,我得到了同样的错误!

java.lang.ClassCastException: com.sun.proxy.$Proxy55 cannot be cast to org.postgresql.jdbc4.Jdbc4Connection

What can I do? 我能做什么?

If you are using javax.sql.DataSource then here is a solution: 如果您使用的是javax.sql.DataSource,那么这是一个解决方案:

if (dataSource.getConnection().isWrapperFor(PGConnection.class)) {
  PGConnection pgConnection = dataSource.getConnection().unwrap(PGConnection.class);
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM