How to convert jdbc4connection to pgconnection?

I want to use Postgres copymanager:

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

Because I use spring boot, the datasource is an org apache. tomcat. jdbc. pool. Datasource, so connect a jdbc4connection

Problem: the conversion throws the following error:

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

In addition, when I tried to cast to jdbc4connection, I got the same error!

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

What can I do?

Solution

If you are using javax sql. Datasource, then this is a solution:

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

I hope this will help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>