From infovaibhav@yahoo.com Fri Jan 15 11:52:13 2010 Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id B7455632601 for ; Fri, 15 Jan 2010 07:52:13 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14520-02 for ; Fri, 15 Jan 2010 11:52:02 +0000 (UTC) X-Greylist: delayed 00:06:40.417383 by SQLgrey-1.7.6 Received: from web94901.mail.in2.yahoo.com (web94901.mail.in2.yahoo.com [203.104.17.136]) by mail.postgresql.org (Postfix) with SMTP id 4D718632217 for ; Fri, 15 Jan 2010 07:52:01 -0400 (AST) Received: (qmail 27460 invoked by uid 60001); 15 Jan 2010 11:45:16 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1263555916; bh=XKDeYPFwqkr8boleaaaKtfCfW2EwRvRqLJru1emJRIo=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=VXOzjMi8XbFtz3Zakk/PoMM+lntPZa8BYmYOgscdeRvI8PV4kaKiqDAcCAaNFO1mke9hMr9+568QVRvwQbbxi/4mcr1lLHM49tGX09GDx3OkAE6OYP8Ahz/n4Gxng4fmKXI8Np0wBOn+729jdTZA7r6ou3A95H1Qt/RlUDpgehM= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type; b=gbX7qlV/lHH6fCl1HjbzqRrknJbwI8g9M+ZOsN4j+D6gIb8/6Stky1aIPqtktFaTrrqnZQhF1vawA4n0huLlXgedxjcXnETQOPu7FsI1o7XOyKwz2zkk+Wnzg2p/wD7z1QGQdvO+dL3DkOS/IxfDAcXRcrMFfQ/BVB/rI59u8vM=; Message-ID: <170472.26453.qm@web94901.mail.in2.yahoo.com> X-YMail-OSG: BgjrZxwVM1kzCryuLfaOP..7t_scA2Nvj9REPJQNCgFQLHoOH0YFtoi6GpvZr8.k.y28qYvB9eNMr23QOoMQC9zC7vywIgEzswC_6Q.kl8UraIkzkGl3DhNMqjKrqOi0X9.7osLIYDbekOriTonQAN75jMXjhydSLgEh_kKKKPguP6_UWIF6HtzfvFkRlauhGa8rpDMwhgRiV2BjnaDVJUFuqmi96ZPmJfnhv08133_INJlpwemHgTvsgHKXxerq8updYCY32.Cjichz5x7F72U.o1BImk7i2mjQptPWq2OeWtTPN5MQqkeCnr9EGm2O11wYIYlE0a0RnDY1c_jIpXVieOE- Received: from [202.71.143.50] by web94901.mail.in2.yahoo.com via HTTP; Fri, 15 Jan 2010 17:15:15 IST X-Mailer: YahooMailRC/272.7 YahooMailWebService/0.8.100.260964 Date: Fri, 15 Jan 2010 17:15:15 +0530 (IST) From: Vaibhav Patil Subject: COPY using Hibernate To: pgsql-jdbc@postgresql.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1757666603-1263555915=:26453" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.002 tagged_above=-10 required=5 tests=BAYES_50=0.001, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201001/21 X-Sequence-Number: 20217 --0-1757666603-1263555915=:26453 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello All,=0A=0AI am using C3p0 for connection pooling using hibernate. The= connection object returned by that pool does not provide the COPY from STD= IN functionality. Have also checked with the apache dbcp pooling mechanism.= I am using JDBC4 driver with JDK1.6.=0AAs I understand, to support COPY op= eration of postgres using JDBC, the connection object should be an implemen= tation of "PGConnection" interface.=0A=0AI am able to perform the COPY oper= ation if I do not use any connection pooling.=0A=0AI am curious to know, if= postgres provide some sort of connection pooling API which I can integrate= in Hibernate and then use the COPY operation ? =0A=0AThanks,=0AVaibhav.=0A= =0A=0A=0A The INTERNET now has a personality. YOURS! See your Yahoo! H= omepage. http://in.yahoo.com/ --0-1757666603-1263555915=:26453 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello All,

I am using C3p0 for connection pooling = using hibernate. The connection object returned by that pool does not provi= de the COPY from STDIN functionality. Have also checked with the apache dbc= p pooling mechanism. I am using JDBC4 driver with JDK1.6.
As I understan= d, to support COPY operation of postgres using JDBC, the connection object = should be an implementation of "PGConnection" interface.

I am able t= o perform the COPY operation if I do not use any connection pooling.
I am curious to know, if postgres provide some sort of connection pooling = API which I can integrate in Hibernate and then use the COPY operation ?
Thanks,
Vaibhav.
=0A=0A

=0A=0A=0A=0A
Thank you very much for you replies, I'll try these solut= ions as well and will get back to you.

The other way I am trying is = the pg/plsql stored procedure. As per the requirement, I have to insert aro= und a million integers in a table having just one column. I am not aware of= how to use "COPY FROM STDIN" using pg/plsql. I'll pass these integers as a= rray to stored procedure. I want to avoid dependency of file, otherwise it = could have been done easily using "COPY FROM file". Looking for more inform= ation on internet to solve the issue. If anybody aware of the way, please h= elp me.

Thanks,
Vaibhav.

From: Steve Waldman <swaldman@mchange.com>
To: Craig Ringer <craig@postnewspapers.com.au= >
Cc: Dave Cramer &l= t;pg@fastcrypt.com>; Vaibhav Patil <infovaibhav@yahoo.com>; "pgsql= -jdbc@postgresql.org" <pgsql-jdbc@postgresql.org>
Sent: Fri, 15 January, 2010 7:56:21 PMSubject: Re: [JDBC] COPY= using Hibernate

you can just check out the inner Connection = with c3p0's reflective API, if you want. the preachiness is a speedbump, bu= t nothing prevents you from returning RAW_CONNECTION_OBJECT. just be sure t= hat when you're done, the you've not left the state of the Connection modif= ied, cuz that can lead to subtle misbehavior.

~oo~
Steve Waldman<= br>swaldman@mchange.com


On Jan 15, 2010, at 7:59 AM, Craig R= inger <craig@postnewspapers.com.au> wrote:
=
> On 15/01/2010 8:00 PM, Dave Cramer wrote:
>> Hi Vaibhav>>
>> C3p0 provides a mechanism to get at the underlying = connection and
>> statement. search for c3P0 underlying connection=
>
> According to the C3P0 docs:
>
> "JDBC driver= s sometimes define vendor-specific, non-standard API on Connection and Stat= ement implementations. C3P0 wraps these Objects behind a proxies, so you ca= nnot cast C3P0-returned Connections or Statements to the vendor-specific im= plementation 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 p= ool corruption."
>
> ... so, you can't just get the connection= from your Hibernate Session and use that. Instead, you have to do it refle= ctively via C3P0 methods:
>
> "C3P0 does provide an API that a= llows you to invoke non-standard methods
> reflectively on an underly= ing Connection. To use it, first cast the
> returned Connection to a = C3P0ProxyConnection. Then call the method
> rawConnectionOperation, s= upplying the java.lang.reflect.Method object
> for the non-standard m= ethod you wish to call as an argument. The Method
> you supply will b= e invoked on the target you provide on the second
> argument (null fo= r static methods), and using the arguments you supply
> in the third = argument to that function. For the target, and for any of
> the metho= d arguments, you can supply the special token
> C3P0ProxyConnection.RAW_CONNECTION, which will be replaced with the
>= ; underlying vendor-specific Connection object before the Method is invoked= ."
>
> See:
>  http://www.mch= ange.com/projects/c3p0/index.html#raw_connection_ops
>
> <= br>> Permit me to say "argh!". It's highly frustrating that you can't ju= st "check out" a connection, unwrapping it and taking responsibility for an= y statements and result sets you create while it's unwrapped.
>
&= gt; ( C3P0's documentation is really preachy about this, and likes to
&g= t;  explain to you how you shouldn't want to do "legacy" things like>  that since it breaks "database independence" which is apparent= ly
>  something it's unthinkable not to care about for your part= icular
>  app ... sigh. )
>
> In my J2SE app I only need one connection for the app - and in fact it's strongly preferabl= e to limit the app to one connection. I also needed direct access to that c= onnection to use listen/notify via PgConnection. Hibernate wants you to use= a connection 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 un= derlying JDBC connection *at* *all*.
>
> Thankfully Hibernate = provides a clean and simple abstraction for its access to connection pools,= so I landed up writing my own SingleConnectionProvider to give Hibernate i= ts "pool" of one connection. The provider blocks on any getConnection(...) = requests issued while the connection is checked out to someone else, so I c= an just check the connection out of the pool directly if I want to do Postg= reSQL-specific things with it ( like using listen/notify 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.
>
> --
> Craig Ringer
> =
> --Sent via pgsql-jdbc mailing list (pgsql-jdbc@post= gresql.org)
> To make changes to your subscription:
> http= ://www.postgresql.org/mailpref/pgsql-jdbc
=0A=0A
=0A=0A=0A=0A
=0AThe INTERNET now has a per= sonality. YOURS! See your Yahoo! Homepage. --0-1198699888-1263566993=:1865-- From craig@postnewspapers.com.au Fri Jan 15 16:21:38 2010 Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 96E216325F4 for ; Fri, 15 Jan 2010 12:21:38 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 55708-05 for ; Fri, 15 Jan 2010 16:21:28 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id C4D2A632217 for ; Fri, 15 Jan 2010 12:21:27 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id A3E521E044A; Sat, 16 Jan 2010 00:21:24 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id AtRcLZbKBiIK; Sat, 16 Jan 2010 00:21:23 +0800 (WST) Received: from [10.1.1.5] (203-206-118-60.dyn.iinet.net.au [203.206.118.60]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 07A0A1E0449; Sat, 16 Jan 2010 00:21:23 +0800 (WST) Message-ID: <4B5095F5.20508@postnewspapers.com.au> Date: Sat, 16 Jan 2010 00:21:09 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Steve Waldman CC: Dave Cramer , Vaibhav Patil , "pgsql-jdbc@postgresql.org" Subject: Re: COPY using Hibernate References: <170472.26453.qm@web94901.mail.in2.yahoo.com> <491f66a51001150400sf2c6b04l840af9f01c3e55bb@mail.gmail.com> <4B5066B1.6050709@postnewspapers.com.au> <82F2099D-4E4C-4073-A900-56A08DDEFD34@mchange.com> In-Reply-To: <82F2099D-4E4C-4073-A900-56A08DDEFD34@mchange.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.185 tagged_above=-10 required=5 tests=BAYES_40=-0.185 X-Spam-Level: X-Archive-Number: 201001/26 X-Sequence-Number: 20222 Steve Waldman wrote: > you can just check out the inner Connection with c3p0's reflective API, > if you want. the preachiness is a speedbump, but nothing prevents you > from returning RAW_CONNECTION_OBJECT. just be sure that when you're > done, the you've not left the state of the Connection modified, cuz that > can lead to subtle misbehavior. With Hibernate's Session object, sure - you can getConnection() from the session, then reflectively access the methods of the underlying PGConnection via C3P0. It's ugly but works. I was about to say that you can't get the Connection object from EntityManager if using Hibernate, when I realised you can call EntityManager.getDelegate() to get the Session, then getConnection() on that. I have no idea how I missed getDelegate() before. Sigh. Guess you *can* just use C3P0's reflective calls even if using JPA. -- Craig Ringer From craig@postnewspapers.com.au Fri Jan 15 16:32:08 2010 Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id E4D1C632E17 for ; Fri, 15 Jan 2010 12:32: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 33768-01 for ; Fri, 15 Jan 2010 16:31:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 71C1E632DE1 for ; Fri, 15 Jan 2010 12:31:57 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id E54BC1E044A; Sat, 16 Jan 2010 00:31:54 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id OjYoiY6eTP1U; Sat, 16 Jan 2010 00:31:53 +0800 (WST) Received: from [10.1.1.5] (203-206-118-60.dyn.iinet.net.au [203.206.118.60]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 49B6D1E0449; Sat, 16 Jan 2010 00:31:53 +0800 (WST) Message-ID: <4B50986B.4090806@postnewspapers.com.au> Date: Sat, 16 Jan 2010 00:31:39 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Vaibhav Patil CC: "pgsql-jdbc@postgresql.org" Subject: Re: COPY using Hibernate References: <170472.26453.qm@web94901.mail.in2.yahoo.com> <491f66a51001150400sf2c6b04l840af9f01c3e55bb@mail.gmail.com> <4B5066B1.6050709@postnewspapers.com.au> <82F2099D-4E4C-4073-A900-56A08DDEFD34@mchange.com> <399131.1865.qm@web94904.mail.in2.yahoo.com> In-Reply-To: <399131.1865.qm@web94904.mail.in2.yahoo.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.337 tagged_above=-10 required=5 tests=AWL=0.262, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/27 X-Sequence-Number: 20223 Vaibhav Patil wrote: > The other way I am trying is the pg/plsql stored procedure. As per the > requirement, I have to insert around a million integers in a table > having just one column. I am not aware of how to use "COPY FROM STDIN" > using pg/plsql. I'll pass these integers as array to stored procedure. I > want to avoid dependency of file, otherwise it could have been done > easily using "COPY FROM file". Looking for more information on internet > to solve the issue. If anybody aware of the way, please help me. It's a real pity the JDBC driver doesn't support the COPY protocol so you could just do this via PGConnection. There have been patches around for ages ... though I presume there are good reasons why they haven't been merged. Do check to make sure that the stored proc approach is actually faster than just doing batched multi-valued INSERTs. Open a transaction, then prepare a statement like this: INSERT INTO sometable VALUES (?),(?),(?),(?),(?); (but with say 20 parameters - you'll need to play around and see how many is optimal), then add a series of invocations of the prepared statement to a JDBC batch insert/update and execute it. I wouldn't be surprised if that was quite a bit faster than doing it with an array and PL/PgSQL. -- Craig Ringer From msakrejda@truviso.com Fri Jan 15 16:50:02 2010 Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 4C4E3632C94 for ; Fri, 15 Jan 2010 12:50:02 -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 37108-03 for ; Fri, 15 Jan 2010 16:49:51 +0000 (UTC) X-Greylist: delayed 00:07:22.236958 by SQLgrey-1.7.6 Received: from mail-pw0-f44.google.com (mail-pw0-f44.google.com [209.85.160.44]) by mail.postgresql.org (Postfix) with ESMTP id 7F6E6632A49 for ; Fri, 15 Jan 2010 12:49:51 -0400 (AST) Received: by pwi15 with SMTP id 15so412873pwi.3 for ; Fri, 15 Jan 2010 08:49:50 -0800 (PST) MIME-Version: 1.0 Received: by 10.142.55.15 with SMTP id d15mr1799543wfa.54.1263573748226; Fri, 15 Jan 2010 08:42:28 -0800 (PST) In-Reply-To: <4B50986B.4090806@postnewspapers.com.au> References: <170472.26453.qm@web94901.mail.in2.yahoo.com> <491f66a51001150400sf2c6b04l840af9f01c3e55bb@mail.gmail.com> <4B5066B1.6050709@postnewspapers.com.au> <82F2099D-4E4C-4073-A900-56A08DDEFD34@mchange.com> <399131.1865.qm@web94904.mail.in2.yahoo.com> <4B50986B.4090806@postnewspapers.com.au> Date: Fri, 15 Jan 2010 08:42:28 -0800 Message-ID: <895e58dd1001150842g6c9784c8r9bf78f5a7ce6f033@mail.gmail.com> Subject: Re: COPY using Hibernate From: Maciek Sakrejda To: Craig Ringer Cc: Vaibhav Patil , "pgsql-jdbc@postgresql.org" Content-Type: text/plain; charset=ISO-8859-1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.525 tagged_above=-10 required=5 tests=AWL=0.074, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/28 X-Sequence-Number: 20224 The driver supports COPY as of 8.4-701. I finally (after much too long a delay; I apologize again) made good on my promise to clean up Kalle Hallivuori's patch with Truviso's internal fixes, and worked with Kris to get it accepted last spring. The final version came out in July. It looks like it's not there in the main documentation, but it *is* in the javadoc. PGConection.getCopyAPI() is the entry point. --- Maciek Sakrejda | Software Engineer | Truviso 1065 E. Hillsdale Blvd., Suite 230 Foster City, CA 94404 (650) 242-3500 Main (650) 242-3501 F msakrejda@truviso.com www.truviso.com On Fri, Jan 15, 2010 at 8:31 AM, Craig Ringer wrote: > Vaibhav Patil wrote: > >> The other way I am trying is the pg/plsql stored procedure. As per the >> requirement, I have to insert around a million integers in a table >> having just one column. I am not aware of how to use "COPY FROM STDIN" >> using pg/plsql. I'll pass these integers as array to stored procedure. I >> want to avoid dependency of file, otherwise it could have been done >> easily using "COPY FROM file". Looking for more information on internet >> to solve the issue. If anybody aware of the way, please help me. > > It's a real pity the JDBC driver doesn't support the COPY protocol so > you could just do this via PGConnection. There have been patches around > for ages ... though I presume there are good reasons why they haven't > been merged. > > Do check to make sure that the stored proc approach is actually faster > than just doing batched multi-valued INSERTs. Open a transaction, then > prepare a statement like this: > > INSERT INTO sometable VALUES (?),(?),(?),(?),(?); > > (but with say 20 parameters - you'll need to play around and see how > many is optimal), then add a series of invocations of the prepared > statement to a JDBC batch insert/update and execute it. > > I wouldn't be surprised if that was quite a bit faster than doing it > with an array and PL/PgSQL. > > -- > Craig Ringer > > -- > Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-jdbc > From craig@postnewspapers.com.au Sat Jan 16 01:11:49 2010 Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 60C9B632C94 for ; Fri, 15 Jan 2010 21:11:49 -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 31147-01 for ; Sat, 16 Jan 2010 01:11:31 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by mail.postgresql.org (Postfix) with ESMTP id 256CD637673 for ; Fri, 15 Jan 2010 21:11:38 -0400 (AST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 1C1C71E044A; Sat, 16 Jan 2010 09:11:34 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 4TX2esDfCOr6; Sat, 16 Jan 2010 09:11:32 +0800 (WST) Received: from [10.1.1.5] (203-206-118-60.dyn.iinet.net.au [203.206.118.60]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id D1E511E0449; Sat, 16 Jan 2010 09:11:31 +0800 (WST) Message-ID: <4B511242.1080904@postnewspapers.com.au> Date: Sat, 16 Jan 2010 09:11:30 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: Maciek Sakrejda CC: Vaibhav Patil , "pgsql-jdbc@postgresql.org" Subject: Re: COPY using Hibernate References: <170472.26453.qm@web94901.mail.in2.yahoo.com> <491f66a51001150400sf2c6b04l840af9f01c3e55bb@mail.gmail.com> <4B5066B1.6050709@postnewspapers.com.au> <82F2099D-4E4C-4073-A900-56A08DDEFD34@mchange.com> <399131.1865.qm@web94904.mail.in2.yahoo.com> <4B50986B.4090806@postnewspapers.com.au> <895e58dd1001150842g6c9784c8r9bf78f5a7ce6f033@mail.gmail.com> In-Reply-To: <895e58dd1001150842g6c9784c8r9bf78f5a7ce6f033@mail.gmail.com> X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.604 tagged_above=-10 required=5 tests=AWL=-0.494, BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 201001/33 X-Sequence-Number: 20229 Maciek Sakrejda wrote: > The driver supports COPY as of 8.4-701. I finally (after much too long > a delay; I apologize again) made good on my promise to clean up Kalle > Hallivuori's patch with Truviso's internal fixes, and worked with Kris > to get it accepted last spring. The final version came out in July. Thanks for that - that's absolutely wonderful to have. -- Craig Ringer From infovaibhav@yahoo.com Mon Jan 18 10:08:08 2010 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--