Re: Cast java.sql.Connection to PGConnection

From: Vitalii Tymchyshyn <vit(at)tym(dot)im>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Philippe Marschall <Philippe(dot)Marschall(at)netcetera(dot)com>
Cc: "peter(dot)penzov" <peter(dot)penzov(at)gmail(dot)com>, "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Cast java.sql.Connection to PGConnection
Date: 2016-05-18 13:28:50
Message-ID: CABWW-d0oohfz3mFVB36utN9KKtA-dtoTyRnXRXkaL9dRF3Qxnw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

I'd really suspect a classloading issue here. TS gets connection through
JNDI and then tries to unwrap/cast. By any chance, have you got two
postgresql driver jars in different classloaders (e.g. two webapps or
tomcat lib and webapp or tomcat common lib)?

Best regards, Vitalii Tymchyshyn

Вт, 17 трав. 2016 17:06 користувач David G. Johnston <
david(dot)g(dot)johnston(at)gmail(dot)com> пише:

> Except that didn't appear to work ... haven't confirmed the behavior
> myself.
>
> In any case an apparent second report of this just surfaced:
>
>
> http://www.postgresql.org/message-id/CANOVrEhYbmO=mJT==YA=JZ_ZYZHEuq_wra5eYOQuZ+xF1feiHA@mail.gmail.com
>
> David J.
>
> On Tue, May 3, 2016 at 3:18 AM, Philippe Marschall <
> Philippe(dot)Marschall(at)netcetera(dot)com> wrote:
>
>> Hi
>>
>> The following is the way to do it:
>>
>> PGConnection pgCon = conn.unwrap(PGConnection.class);
>>
>> Cheers
>> Philippe
>> ________________________________________
>> From: pgsql-jdbc-owner(at)postgresql(dot)org <pgsql-jdbc-owner(at)postgresql(dot)org>
>> on behalf of peter.penzov <peter(dot)penzov(at)gmail(dot)com>
>> Sent: Monday, May 2, 2016 20:29
>> To: pgsql-jdbc(at)postgresql(dot)org
>> Subject: [JDBC] Cast java.sql.Connection to PGConnection
>>
>> 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.
>>
>>
>> --
>> Sent via pgsql-jdbc mailing list (pgsql-jdbc(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>
>>
>> --
>> Sent via pgsql-jdbc mailing list (pgsql-jdbc(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-jdbc
>>
>
>

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Eric Neron 2016-05-18 14:49:31 Re: Cast java.sql.Connection to PGConnection
Previous Message Vladimir Sitnikov 2016-05-18 06:07:04 Re: Tomcat JDBC Connection Pool interfaces to PostgreSQL...