Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E01A762E85C for ; Mon, 18 Jan 2010 06:08:08 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 30847-04 for ; Mon, 18 Jan 2010 10:07:53 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from web94905.mail.in2.yahoo.com (web94905.mail.in2.yahoo.com [203.104.17.152]) by mail.postgresql.org (Postfix) with SMTP id F162C632281 for ; Mon, 18 Jan 2010 06:07:56 -0400 (AST) Received: (qmail 48516 invoked by uid 60001); 18 Jan 2010 10:07:53 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1263809272; bh=DKh0+Sttj+rpjA7HYkng4fhl84pTKCLXbbyRYXOZ1sw=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=Sz2w0gpGHRVDQVaPnSTB8+g5yPMtR6k/PFetWnN57+X+jKxukkaP0jnE8ZVPEPTah5BQwgEQUwHTcgtKq0L5TuShOaCfk9MCNkgtmH7QREWtn+mYnjkLIq1BNYR21/7Z9533ra8nPlSkDB62axdAnfor7jrp3sIsdomvjUPO8l0= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=TGaS/R2La4P3aLrIGqvRdAlSKbTpfdUa97vsOd8eMNRJawFPv+f34I+G0OhRlo4V2VEnpNwPR1rJB+Xi28Y7Bl5X9Nc7If+tyrBf+ePxAjYEYVjl+5lkia8nB/UZuQ6pAhKmfATWHzUkjD20z6fd4ECqHYOIN3qfPGpLsMuddeQ=; Message-ID: <982438.48282.qm@web94905.mail.in2.yahoo.com> X-YMail-OSG: ERPL.s0VM1m.e8yi7cSX40vNuj9iJMol6jaZn9HesdwATOA4A9FeKUINOlcXYBGzHbTuQvHHX_YUYzJ7_d4ZqZYyW.FAvwRwXQ4Wc8HlXDdvczWBSUOt1BU14GdWBubme5Hi7fW6kOWfB_s3QzX0_QTeE2rnC2QDdpOPPAxjeiOQVWp5d65PL8gI9y_GEruvxw1gi74Y.tVyal75zg7eNUTqDz0qVq83jfq_MlWQXWb2TYLtx7y_U6axPMS6i_G3jsGy9I5c0pdN3as4swYWRNieIAyVt7o_ZLGMnk5HGfMQOfNFSKXBr2XNrrE0uzuHCQEXBB_DfFoTyGFza9ny1Ae4t74- Received: from [202.71.143.50] by web94905.mail.in2.yahoo.com via HTTP; Mon, 18 Jan 2010 15:37:52 IST X-Mailer: YahooMailRC/272.7 YahooMailWebService/0.8.100.260964 References: <170472.26453.qm@web94901.mail.in2.yahoo.com> <491f66a51001150400sf2c6b04l840af9f01c3e55bb@mail.gmail.com> <4B5066B1.6050709@postnewspapers.com.au> Date: Mon, 18 Jan 2010 15:37:52 +0530 (IST) From: Vaibhav Patil Subject: Re: COPY using Hibernate To: pgsql-jdbc@postgresql.org In-Reply-To: <4B5066B1.6050709@postnewspapers.com.au> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1666795976-1263809272=:48282" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201001/46 X-Sequence-Number: 20242 --0-1666795976-1263809272=:48282 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Thanks Craig,=0AThe way of invoking methods on the underlying connection us= ing reflection provided by C3P0 API worked for me.=0AHere is the piece of c= ode I wrote - =0A C3P0ProxyConnection con =3D (C3P0ProxyConnection)sessio= n.connection(); =0A Method m =3D BaseConnection.class.getMethod("= getCopyAPI", new Class[]{});=0A Object[] arg =3D new Object[] {};=0A = CopyManager manager =3D (CopyManager) con.rawConnectionOperation(m, C3P0Pro= xyConnection.RAW_CONNECTION, arg);=0AI can use this instance of CopyManager= for further copy operation.=0A=0A-- Vaibhav Patil.=0A=0A=0A=0A____________= ____________________=0AFrom: Craig Ringer =0AT= o: Dave Cramer =0ACc: Vaibhav Patil ; pgsql-jdbc@postgresql.org=0ASent: Fri, 15 January, 2010 6:29:29 PM=0ASu= bject: Re: [JDBC] COPY using Hibernate=0A=0AOn 15/01/2010 8:00 PM, Dave Cra= mer wrote:=0A> Hi Vaibhav=0A> =0A> C3p0 provides a mechanism to get at the = underlying connection and=0A> statement. search for c3P0 underlying connect= ion=0A=0AAccording to the C3P0 docs:=0A=0A"JDBC drivers sometimes define ve= ndor-specific, non-standard API on Connection and Statement implementations= . C3P0 wraps these Objects behind a proxies, so you cannot cast C3P0-return= ed Connections or Statements to the vendor-specific implementation classes.= C3P0 does not provide any means of accessing the raw Connections and State= ments directly, because C3P0 needs to keep track of Statements and ResultSe= ts created in order to prevent resource leaks and pool corruption."=0A=0A..= . so, you can't just get the connection from your Hibernate Session and use= that. Instead, you have to do it reflectively via C3P0 methods:=0A=0A"C3P0= does provide an API that allows you to invoke non-standard methods=0Arefle= ctively on an underlying Connection. To use it, first cast the=0Areturned C= onnection to a C3P0ProxyConnection. Then call the method=0ArawConnectionOpe= ration, supplying the java.lang.reflect.Method object=0Afor the non-standar= d method you wish to call as an argument. The Method=0Ayou supply will be i= nvoked on the target you provide on the second=0Aargument (null for static = methods), and using the arguments you supply=0Ain the third argument to tha= t function. For the target, and for any of=0Athe method arguments, you can = supply the special token=0AC3P0ProxyConnection.RAW_CONNECTION, which will b= e replaced with the=0Aunderlying vendor-specific Connection object before t= he Method is invoked."=0A=0ASee:=0A http://www.mchange.com/projects/c3p0/i= ndex.html#raw_connection_ops=0A=0A=0APermit me to say "argh!". It's highly = frustrating that you can't just "check out" a connection, unwrapping it and= taking responsibility for any statements and result sets you create while = it's unwrapped.=0A=0A( C3P0's documentation is really preachy about this, a= nd likes to=0A explain to you how you shouldn't want to do "legacy" things= like=0A that since it breaks "database independence" which is apparently= =0A something it's unthinkable not to care about for your particular=0A a= pp ... sigh. )=0A=0AIn my J2SE app I only need one connection for the app -= and in fact it's strongly preferable to limit the app to one connection. I= also needed direct access to that connection to use listen/notify via PgCo= nnection. Hibernate wants you to use a connection pool, and jealously guard= s the connections it obtains via the pool - in fact, if you're using Hibern= ate via JPA2 you can't access the underlying JDBC connection *at* *all*.=0A= =0AThankfully Hibernate provides a clean and simple abstraction for its acc= ess to connection pools, so I landed up writing my own SingleConnectionProv= ider to give Hibernate its "pool" of one connection. The provider blocks on= any getConnection(...) requests issued while the connection is checked out= to someone else, so I can just check the connection out of the pool direct= ly if I want to do PostgreSQL-specific things with it ( like using listen/n= otify or COPY ) and use Hibernate the rest of the time. It works great. If = it'd be of any use to you, let me know.=0A=0A--=0ACraig Ringer=0A=0A=0A=0A = The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. h= ttp://in.yahoo.com/ --0-1666795976-1263809272=:48282 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Thanks Craig,
The = way of invoking methods on the underlying connection using reflection provi= ded by C3P0 API worked for me.
Here is the piece of code I wrote -
&= nbsp;   C3P0ProxyConnection co= n =3D (C3P0ProxyConnection)session.connection();     &n= bsp; 
    Method m =3D BaseConnection.class.getMethod("= getCopyAPI", new Class[]{});
    Object[] arg =3D new Object[= ] {};
    CopyManager manager =3D (CopyManager) con.rawConnectionOperation(m, C3P0Pr= oxyConnection.RAW_CONNECTION, arg);
I can use this instance of Co= pyManager for further copy operation.

-- Vaibhav
Patil.
=
From: Craig Ringer <craig@postnewspapers.com.au>= ;
To: Dave Cramer <p= g@fastcrypt.com>
Cc:= Vaibhav Patil <infovaibhav@yahoo.com>; pgsql-jdbc@postgresql.org
= Sent: Fri, 15 January, 201= 0 6:29:29 PM
Subject: R= e: [JDBC] COPY using Hibernate

On 15/01/2010 8:00 PM, Dave Cr= amer wrote:
> Hi Vaibhav
>
> C3p0 provides a mechanism t= o get at the underlying connection and
> statement. search for c3P0 underlying co= nnection

According to the C3P0 docs:

"JDBC drivers sometimes = define vendor-specific, non-standard API on Connection and Statement implem= entations. C3P0 wraps these Objects behind a proxies, so you cannot cast C3= P0-returned Connections or Statements to the vendor-specific implementation= classes. C3P0 does not provide any means of accessing the raw Connections = and Statements directly, because C3P0 needs to keep track of Statements and= ResultSets created in order to prevent resource leaks and pool corruption.= "

... so, you can't just get the connection from your Hibernate Sess= ion and use that. Instead, you have to do it reflectively via C3P0 methods:=

"C3P0 does provide an API that allows you to invoke non-standard me= thods
reflectively on an underlying Connection. To use it, first cast th= e
returned Connection to a C3P0ProxyConnection. Then call the method
rawConnectionOperation, supplying the java.lang.reflect.Method o= bject
for the non-standard method you wish to call as an argument. The M= ethod
you supply will be invoked on the target you provide on the second=
argument (null for static methods), and using the arguments you supply<= br>in the third argument to that function. For the target, and for any ofthe method arguments, you can supply the special token
C3P0ProxyConnec= tion.RAW_CONNECTION, which will be replaced with the
underlying vendor-s= pecific Connection object before the Method is invoked."

See:
&nb= sp; http://www.mchange.com/projects/c3p0/index.html#r= aw_connection_ops


Permit me to say "argh!". It's highly frus= trating that you can't just "check out" a connection, unwrapping it and tak= ing responsibility for any statements and result sets you create while it's unwrapped.

( C3P0's documentation is really preachy about this= , and likes to
  explain to you how you shouldn't want to do "legac= y" things like
  that since it breaks "database independence" which= is apparently
  something it's unthinkable not to care about for y= our particular
  app ... sigh. )

In my J2SE app I only need = one connection for the app - and in fact it's strongly preferable to limit = the app to one connection. I also needed direct access to that connection t= o use listen/notify via PgConnection. Hibernate wants you to use a connecti= on pool, and jealously guards the connections it obtains via the pool - in = fact, if you're using Hibernate via JPA2 you can't access the underlying JD= BC connection *at* *all*.

Thankfully Hibernate provides a clean and = simple abstraction for its access to connection pools, so I landed up writi= ng my own SingleConnectionProvider to give Hibernate its "pool" of one connection. The provider blocks on any getConnection(...) requests iss= ued while the connection is checked out to someone else, so I can just chec= k the connection out of the pool directly if I want to do PostgreSQL-specif= ic things with it ( like using listen/notify or COPY ) and use Hibernate th= e rest of the time. It works great. If it'd be of any use to you, let me kn= ow.

--
Craig Ringer
=0A=0A

=0A=0A=0A=0A =
=0AThe INTERNET now has a personality. YOURS!= See your Yahoo! Homepage. --0-1666795976-1263809272=:48282--