From: | "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com> |
---|---|
To: | "peter(dot)penzov" <peter(dot)penzov(at)gmail(dot)com> |
Cc: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Re: Cast java.sql.Connection to PGConnection |
Date: | 2016-05-03 01:05:26 |
Message-ID: | CAKFQuwYOvib8hDfcpAJJXXsO32Jbt+1NFQHksNqCbLTaEwE3Hw@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
I'm having trouble deciding which of the three emails from you to reply
to.....
On Mon, May 2, 2016 at 11:29 AM, peter.penzov <peter(dot)penzov(at)gmail(dot)com>
wrote:
>
>
> 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
>
>
OK. So whatever Tomcat is doing under the hood its giving you a proxy
object. Given how connection pooling works this is not surprising. The
fact that it cannot be cast to org.postgresql.PGConnection is likewise
unsurprising. You are dealing with composition, not extension.
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
>
No help here. This isn't technically the correct list for problems about
Tomcat classes though hopefully enough people on here actually use this
stuff and can provide meaningful help. Having not encountered this before
I am not one of those people.
I am confused how you call unwrap(Connection.class) and get an exception
naming org.postgresql.PGConnection explicitly....
> 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();
>
>
Given the SQLException during the unwrap above this too is not surprising.
Somwhere in the wrapping hierarchy someone seems to not be implementing
the java.sql.Wrapper interface correctly or usefully.
https://docs.oracle.com/javase/8/docs/api/java/sql/Wrapper.html#unwrap-java.lang.Class-
PGConnection is at the end of this chain there isn't much the project
itself can do.
David J.
From | Date | Subject | |
---|---|---|---|
Next Message | Gavin Flower | 2016-05-03 01:48:23 | Re: pgsql jdbc db connection weird behaviour |
Previous Message | Dave Cramer | 2016-05-03 00:38:16 | Re: PGConnection (LargeObject) / JNDI Tomcat Issue |