Cast java.sql.Connection to PGConnection - Mailing list pgsql-jdbc

From peter.penzov
Subject Cast java.sql.Connection to PGConnection
Date
Msg-id 1462213750076-5901531.post@n5.nabble.com
Whole thread Raw
Responses Re: Cast java.sql.Connection to PGConnection  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: Cast java.sql.Connection to PGConnection  (Philippe Marschall <Philippe.Marschall@netcetera.com>)
List pgsql-jdbc

I want to upload a file in PostgreSQL using Tomcat:

@Resource(name = "jdbc/DefaultDB")
private DataSource ds;
Connection conn = ds.getConnection();

I tried to cast the conn object this way:

PGConnection pgCon = ((org.apache.commons.dbcp.DelegatingConnection)
conn).getInnermostDelegate();

I get

Caused by: java.lang.ClassCastException:
org.apache.tomcat.dbcp.dbcp2.PoolingDataSource$PoolGuardConnectionWrapper
cannot be cast to org.postgresql.PGConnection

I also tried:

Connection unwrap = conn.unwrap(Connection.class);
connSec = (org.postgresql.PGConnection) unwrap;

I get

java.sql.SQLException: Cannot unwrap to org.postgresql.PGConnection

LargeObjectManager pgCon = pgCon.getLargeObjectAPI();

What is the proper way to implement the code?

P.S

I tried this:

PGConnection pgConnection = null;

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

LargeObjectManager lobj = pgConnection.getLargeObjectAPI();

But I get NPE at this line LargeObjectManager lobj =
pgConnection.getLargeObjectAPI();




--
View this message in context: http://postgresql.nabble.com/Cast-java-sql-Connection-to-PGConnection-tp5901531.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


pgsql-jdbc by date:

Previous
From: Dave Cramer
Date:
Subject: Re: DatabaseMetaData.getColumns does not return full qualified type
Next
From: "peter.penzov"
Date:
Subject: Re: PGConnection (LargeObject) / JNDI Tomcat Issue