X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5E25452991 for ; Tue, 28 Jun 2005 17:54:00 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 47125-02 for ; Tue, 28 Jun 2005 20:53:58 +0000 (GMT) Received: from mir3-fs.mir3.com (www.mir3.com [216.74.11.46]) by svr1.postgresql.org (Postfix) with ESMTP id 3D699529B8 for ; Tue, 28 Jun 2005 17:53:57 -0300 (ADT) Received: from archimedes.mirlogic.com ([172.16.2.68]) by mir3-fs.mir3.com with Microsoft SMTPSVC(5.0.2195.6713); Tue, 28 Jun 2005 13:32:29 -0700 Subject: Re: OT: easiest way to create a custom PreparedStatement From: Mark Lewis To: Kevin Murphy Cc: pgsql-jdbc@postgresql.org In-Reply-To: <200506281455.09688.cservin@cromagnon.com> References: <42C19D1D.30809@genome.chop.edu> <200506281455.09688.cservin@cromagnon.com> Content-Type: multipart/alternative; boundary="=-aEmIh+QXHVAE7vAszdcp" Organization: MIR3, Inc. Date: Tue, 28 Jun 2005 13:29:42 -0700 Message-Id: <1119990583.698.96.camel@archimedes.mirlogic.com> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-16) X-OriginalArrivalTime: 28 Jun 2005 20:32:29.0342 (UTC) FILETIME=[80E81BE0:01C57C20] X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200506/148 X-Sequence-Number: 13110 --=-aEmIh+QXHVAE7vAszdcp Content-Type: text/plain Content-Transfer-Encoding: 7bit We use java.lang.reflect.Proxy in our production systems to do things like log SQL statements, do a little bookkeeping for application-level replication, log timings and the like. Works great. Here's a snippet that we use to wrap a PreparedStatement with a proxy to do some logging every time somebody calls any method on it. -- Mark Lewis final PreparedStatement realStatement = xxx; PreparedStatement debugWrapped = (PreparedStatement)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{PreparedStatement.class}, new InvocationHandler() { public Object invoke(Object instance, Method method, Object [] params) throws Throwable { StringBuffer buf = new StringBuffer(); buf.append("Called ").append(method.getName()); buf.append('('); if(params != null) { for(int i=0; i 0) buf.append(','); buf.append(String.valueOf(params[i])); } } buf.append(')'); log.debug(buf); try { return method.invoke(realStatement, params); } catch(InvocationTargetException ex) { throw ex.getCause(); } } }); On Tue, 2005-06-28 at 14:55 -0500, Craig Servin wrote: > Or you could use: > > java.lang.reflect.Proxy > > > On Tuesday 28 June 2005 01:55 pm, Kevin Murphy wrote: > > I am modifying some existing data loading code (that only makes use of 4 > > or 5 methods from PreparedStatement) to optionally be able to *not* > > connect to a database but instead dump either the generated SQL code or > > the raw insert data to files on the file system. > > > > It would be great if I could just overload PreparedStatement with my own > > classes, but unfortunately it is an interface, so I presumably need to > > implement stubs for the umpty-ump methods defined by PreparedStatement? > > > > Is this true, and if so, what is the easiest/fastest way to do this (if > > it is even possible)? > > > > I am a java newbie, by the way. Don't flame me too hard. I did say > > "OT", after all ;-) > > > > Thanks, > > Kevin > > > > > > ---------------------------(end of broadcast)--------------------------- > > TIP 3: if posting/reading through Usenet, please send an appropriate > > subscribe-nomail command to majordomo@postgresql.org so that your > > message can get through to the mailing list cleanly > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster --=-aEmIh+QXHVAE7vAszdcp Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit We use java.lang.reflect.Proxy in our production systems to do things like log SQL statements, do a little bookkeeping for application-level replication, log timings and the like.  Works great.

Here's a snippet that we use to wrap a PreparedStatement with a proxy to do some logging every time somebody calls any method on it.

-- Mark Lewis


         final PreparedStatement realStatement = xxx;

        PreparedStatement debugWrapped = (PreparedStatement)Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{PreparedStatement.class}, new InvocationHandler() {
            public Object invoke(Object instance, Method method, Object [] params) throws Throwable {
            StringBuffer buf = new StringBuffer();
            buf.append("Called ").append(method.getName());
            buf.append('(');
            if(params != null) {
            for(int i=0; i<params.length; i++) {
            if(i > 0) buf.append(',');
            buf.append(String.valueOf(params[i]));
            }
            }
            buf.append(')');
            log.debug(buf);
            try {
            return method.invoke(realStatement, params);
            }
            catch(InvocationTargetException ex) {
            throw ex.getCause();
            }
            }
            });




On Tue, 2005-06-28 at 14:55 -0500, Craig Servin wrote:
Or you could use:

java.lang.reflect.Proxy


On Tuesday 28 June 2005 01:55 pm, Kevin Murphy wrote:
> I am modifying some existing data loading code (that only makes use of 4
> or 5 methods from PreparedStatement) to optionally be able to *not*
> connect to a database but instead dump either the generated SQL code or
> the raw insert data to files on the file system.
>
> It would be great if I could just overload PreparedStatement with my own
> classes, but unfortunately it is an interface, so I presumably need to
> implement stubs for the umpty-ump methods defined by PreparedStatement?
>
> Is this true, and if so, what is the easiest/fastest way to do this (if
> it is even possible)?
>
> I am a java newbie, by the way.  Don't flame me too hard.  I did say
> "OT", after all ;-)
>
> Thanks,
> Kevin
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>        subscribe-nomail command to majordomo@postgresql.org so that your
>        message can get through to the mailing list cleanly

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster
--=-aEmIh+QXHVAE7vAszdcp--