pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philippe Marschall <Philippe.Marschall@netcetera.com>
To: peter.penzov <peter.penzov@gmail.com>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: Cast java.sql.Connection to PGConnection
Date: Tue, 3 May 2016 07:18:55 +0000
Message-ID: <1462259930424.38899@netcetera.com> (raw)
In-Reply-To: <1462213750076-5901531.post@n5.nabble.com>
References: <1462213750076-5901531.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>

Hi

The following is the way to do it:

PGConnection pgCon = conn.unwrap(PGConnection.class);

Cheers
Philippe
________________________________________
From: pgsql-jdbc-owner@postgresql.org <pgsql-jdbc-owner@postgresql.org> on behalf of peter.penzov <peter.penzov@gmail.com>
Sent: Monday, May 2, 2016 20:29
To: pgsql-jdbc@postgresql.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@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc


-- 
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc



view thread (8+ messages)  latest in thread

Message-ID: <1462259930424.38899@netcetera.com>
Permalink:  ../1462259930424.38899@netcetera.com/
Also on:    postgresql.org/message-id/1462259930424.38899@netcetera.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: Philippe.Marschall@netcetera.com, peter.penzov@gmail.com
  Subject: Re: Cast java.sql.Connection to PGConnection
  In-Reply-To: <1462259930424.38899@netcetera.com>
  References: <1462213750076-5901531.post@n5.nabble.com> <1462259930424.38899@netcetera.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox