pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: peter.penzov <peter.penzov@gmail.com>
To: pgsql-jdbc@postgresql.org
Subject: Cast java.sql.Connection to PGConnection
Date: Mon, 2 May 2016 11:29:10 -0700 (MST)
Message-ID: <1462213750076-5901531.post@n5.nabble.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-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.


-- 
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: <1462213750076-5901531.post@n5.nabble.com>
Permalink:  ../1462213750076-5901531.post@n5.nabble.com/
Also on:    postgresql.org/message-id/1462213750076-5901531.post@n5.nabble.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: peter.penzov@gmail.com
  Subject: Re: Cast java.sql.Connection to PGConnection
  In-Reply-To: <1462213750076-5901531.post@n5.nabble.com>
  References: <1462213750076-5901531.post@n5.nabble.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