Обсуждение: pg jdbc and dbcp error

Поиск
Список
Период
Сортировка

pg jdbc and dbcp error

От
tfinneid@student.matnat.uio.no
Дата:
Hi

I am having problems getting apache DBCP to work with pg jdbc.
It is claiming it can't convert a Connection into a PGConnection.

the sentence throws the exception is something like this:

    Connection con = ds.getConnection()    \\ DBCP BasicDataSource
   ((PGConnection)con).getCopyAPI.copyIntoDB("COPY attr (val1) from
STDIN"), stream);

the error message is (the message is handcopied so I might make mistakes)

ClassCastException:
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper can
not be cast to org.postgresql.PGConnection


Anybody got any ideas whats wrong? the getConnection() returns a
javax.sql.Connection object, so casting it to PGConnection should not be a
problem.

Using the driver
   podtgresql-jdbc-8.2-505-copy-20070719.jdbc3.jar

which contains a pathc for COPY functionality
(http://kato.iki.fi/sw/db/postgresql/jdbc/copy/)


Re: pg jdbc and dbcp error

От
Eric Faulhaber
Дата:
tfinneid@student.matnat.uio.no wrote:
> Hi
>
> I am having problems getting apache DBCP to work with pg jdbc.
> It is claiming it can't convert a Connection into a PGConnection.
>
> the sentence throws the exception is something like this:
>
>     Connection con = ds.getConnection()    \\ DBCP BasicDataSource
>    ((PGConnection)con).getCopyAPI.copyIntoDB("COPY attr (val1) from
> STDIN"), stream);
>
> the error message is (the message is handcopied so I might make mistakes)
>
> ClassCastException:
> org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper can
> not be cast to org.postgresql.PGConnection
>
>
> Anybody got any ideas whats wrong? the getConnection() returns a
> javax.sql.Connection object, so casting it to PGConnection should not be a
> problem.
>
>

This is an incorrect assumption.  Just because PGConnection implements
Connection, it doesn't mean what DBCP is giving you is an instance of
PGConnection.

Evidently ds.getConnection() is handing you back an instance of
org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper,
which implements Connection, but *is not* an instance of PGConnection,
hence the ClassCastException.  I don't use DBCP, but from the name
"PoolGuardConnectionWrapper", it would seem there's an instance of
PGConnection inside this object, to which the wrapper object delegates.

Regards,
Eric Faulhaber

Re: pg jdbc and dbcp error

От
"Mark Lewis"
Дата:
On Mon, 2007-10-01 at 16:20 +0200, tfinneid@student.matnat.uio.no wrote:
> Hi
>
> I am having problems getting apache DBCP to work with pg jdbc.
> It is claiming it can't convert a Connection into a PGConnection.
>
> the sentence throws the exception is something like this:
>
>     Connection con = ds.getConnection()    \\ DBCP BasicDataSource
>    ((PGConnection)con).getCopyAPI.copyIntoDB("COPY attr (val1) from
> STDIN"), stream);
>
> the error message is (the message is handcopied so I might make mistakes)
>
> ClassCastException:
> org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper can
> not be cast to org.postgresql.PGConnection
>
>
> Anybody got any ideas whats wrong? the getConnection() returns a
> javax.sql.Connection object, so casting it to PGConnection should not be a
> problem.


DBCP doesn't give you the actual PG connection, instead it gives you a
wrapper connection object which adds connection pooling stuff on top of
the regular PG Connection.  This wrapper implements the Connection
interface, but it isn't a PGConnection, so that's why you get a
ClassCastException.

If you instead cast the connection to a DelegatingConnection (the DBCP
wrapper class), then you can invoke the .getInnermostDelegate() method
and get a handle to the underlying PG Connection object.

-- Mark Lewis



Re: pg jdbc and dbcp error

От
tfinneid@student.matnat.uio.no
Дата:
> If you instead cast the connection to a DelegatingConnection (the DBCP
> wrapper class), then you can invoke the .getInnermostDelegate() method
> and get a handle to the underlying PG Connection object.

Thanks, that cleared up that error, but then I got another error:
NullPointerException

It claims that the delegate does not exist, so it returns null instead.
Any ideas?

(BTW, if anybody knows of a good tutorial on DBCP, i would appreciate it
enormously. The  documentation on the web page of DBCP just confuses me.)

regards

thomas


Re: pg jdbc and dbcp error

От
Mark Lewis
Дата:
On Mon, 2007-10-01 at 17:03 +0200, tfinneid@student.matnat.uio.no wrote:
> > If you instead cast the connection to a DelegatingConnection (the DBCP
> > wrapper class), then you can invoke the .getInnermostDelegate() method
> > and get a handle to the underlying PG Connection object.
>
> Thanks, that cleared up that error, but then I got another error:
> NullPointerException
>
> It claims that the delegate does not exist, so it returns null instead.
> Any ideas?

What is returning null?  You need to be more specific.  Could you post a
code snippet?

Also, this isn't really PG-specific, this is a DBCP issue.  I'll help
you with it if you want, but after this message let's drop it from the
list to avoid cluttering up pg-jdbc.

-- Mark