X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8E33411F655F for ; Thu, 6 Apr 2006 03:54:58 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 64106-04 for ; Thu, 6 Apr 2006 03:54:54 -0300 (ADT) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id B503611F60A5 for ; Thu, 6 Apr 2006 03:54:55 -0300 (ADT) Received: from galaxy.systems.pipex.net (galaxy.systems.pipex.net [62.241.162.31]) by svr4.postgresql.org (Postfix) with ESMTP id B29C95AF8E3 for ; Thu, 6 Apr 2006 06:54:52 +0000 (GMT) Received: from corona (81-179-81-145.dsl.pipex.com [81.179.81.145]) by galaxy.systems.pipex.net (Postfix) with ESMTP id F1071E0002A9; Thu, 6 Apr 2006 07:54:46 +0100 (BST) Subject: [PATCH] Support for ping method. From: Michael Barker To: pgsql-jdbc@postgresql.org Cc: Andy Oliver Content-Type: multipart/mixed; boundary="=-MEC9uoihVGMjAaSJwSuu" Date: Thu, 06 Apr 2006 07:38:49 +0100 Message-Id: <1144305529.2955.11.camel@corona> Mime-Version: 1.0 X-Mailer: Evolution 2.6.0 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200604/54 X-Sequence-Number: 14988 --=-MEC9uoihVGMjAaSJwSuu Content-Type: text/plain Content-Transfer-Encoding: 7bit Hi, Attached is a patch that adds support for a ping method on the AbstractJdbc2Connection. This is useful for JBoss (and other containers) where the container needs to check if the connection is alive before passing it to the caller. Currently JBoss does a "SELECT 1", but using the new ping method there is a boost in performance. Opening and closing 1000 connections from a datasource: PGTest1 (SELECT 1) 1000 iterations: 2491ms PGTest2 (ping()) 1000 iterations: 63ms All is does is send a sendSync and wait for a valid response. Regards, Michael Barker. --=-MEC9uoihVGMjAaSJwSuu Content-Disposition: attachment; filename=ping_patch Content-Type: text/plain; name=ping_patch; charset=us-ascii Content-Transfer-Encoding: 7bit ? bin ? build.local.properties ? ping_patch Index: org/postgresql/core/ProtocolConnection.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/ProtocolConnection.java,v retrieving revision 1.6 diff -u -r1.6 ProtocolConnection.java --- org/postgresql/core/ProtocolConnection.java 21 Jun 2005 18:07:08 -0000 1.6 +++ org/postgresql/core/ProtocolConnection.java 5 Apr 2006 21:22:34 -0000 @@ -120,4 +120,10 @@ * @return the version of the implementation */ public int getProtocolVersion(); + + /** + * All user to ping the connection the database, + * + */ + public int ping(); } Index: org/postgresql/core/v2/ProtocolConnectionImpl.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v2/ProtocolConnectionImpl.java,v retrieving revision 1.9 diff -u -r1.9 ProtocolConnectionImpl.java --- org/postgresql/core/v2/ProtocolConnectionImpl.java 2 Dec 2005 03:05:07 -0000 1.9 +++ org/postgresql/core/v2/ProtocolConnectionImpl.java 5 Apr 2006 21:22:34 -0000 @@ -193,6 +193,12 @@ return 2; } + public int ping() + { + // Not supported in V2. + return 0; + } + private String serverVersion; private int cancelPid; private int cancelKey; Index: org/postgresql/core/v3/ProtocolConnectionImpl.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v3/ProtocolConnectionImpl.java,v retrieving revision 1.10 diff -u -r1.10 ProtocolConnectionImpl.java --- org/postgresql/core/v3/ProtocolConnectionImpl.java 2 Dec 2005 03:05:09 -0000 1.10 +++ org/postgresql/core/v3/ProtocolConnectionImpl.java 5 Apr 2006 21:22:35 -0000 @@ -188,6 +188,11 @@ { return 3; } + + public int ping() + { + return executor.ping(); + } private String serverVersion; private int cancelPid; Index: org/postgresql/core/v3/QueryExecutorImpl.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/v3/QueryExecutorImpl.java,v retrieving revision 1.27 diff -u -r1.27 QueryExecutorImpl.java --- org/postgresql/core/v3/QueryExecutorImpl.java 30 Jan 2006 20:12:06 -0000 1.27 +++ org/postgresql/core/v3/QueryExecutorImpl.java 5 Apr 2006 21:22:37 -0000 @@ -21,6 +21,7 @@ import java.io.IOException; import java.sql.*; + import org.postgresql.Driver; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLWarning; @@ -357,6 +358,33 @@ handler.handleCompletion(); } + + + public int ping() + { + int result = 0; + try + { + sendSync(); + int c = pgStream.ReceiveChar(); + if (c == 'Z') + { + receiveRFQ(); + result = 0; + } + else + { + result = -1; + } + } + catch (IOException ioe) + { + result = -2; + } + + return result; + } + private ResultHandler sendQueryPreamble(final ResultHandler delegateHandler, int flags) throws IOException { // First, send CloseStatements for finalized SimpleQueries that had statement names assigned. Index: org/postgresql/jdbc2/AbstractJdbc2Connection.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2Connection.java,v retrieving revision 1.37 diff -u -r1.37 AbstractJdbc2Connection.java --- org/postgresql/jdbc2/AbstractJdbc2Connection.java 24 Nov 2005 06:44:21 -0000 1.37 +++ org/postgresql/jdbc2/AbstractJdbc2Connection.java 5 Apr 2006 21:22:40 -0000 @@ -1077,4 +1077,16 @@ { return bindStringAsVarchar; } + + /** + * Pings the postgresql database, will return <0 if the + * database is unavailable. + * + * @return + * @throws SQLException + */ + public int ping() + { + return protoConnection.ping(); + } } Index: org/postgresql/test/jdbc2/MiscTest.java =================================================================== RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/MiscTest.java,v retrieving revision 1.20 diff -u -r1.20 MiscTest.java --- org/postgresql/test/jdbc2/MiscTest.java 2 Dec 2005 03:05:10 -0000 1.20 +++ org/postgresql/test/jdbc2/MiscTest.java 5 Apr 2006 21:22:40 -0000 @@ -9,6 +9,7 @@ */ package org.postgresql.test.jdbc2; +import org.postgresql.jdbc2.AbstractJdbc2Connection; import org.postgresql.test.TestUtil; import junit.framework.TestCase; import java.sql.*; @@ -67,6 +68,7 @@ stmt.cancel(); } } + public void testError() throws Exception { @@ -115,6 +117,15 @@ stmt.close(); con.close(); } + + public void testPint() throws Exception + { + Connection con = TestUtil.openDB(); + AbstractJdbc2Connection cn = (AbstractJdbc2Connection) con; + assertEquals(0, cn.ping()); + cn.close(); + } + public void xtestLocking() throws Exception { --=-MEC9uoihVGMjAaSJwSuu--