pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Michael Barker <mike@middlesoft.co.uk>
To: pgsql-jdbc@postgresql.org
Cc: Andy Oliver <acoliver@jboss.org>
Subject: [PATCH] Support for ping method.
Date: Thu, 06 Apr 2006 07:38:49 +0100
Message-ID: <1144305529.2955.11.camel@corona> (raw )
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.
Attachments:
ping_patch (text/plain, 5.2K) download
? 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
{
view thread (5+ messages) latest in thread
Message-ID: <1144305529.2955.11.camel@corona>
Permalink: ../1144305529.2955.11.camel@corona/
Also on: postgresql.org/message-id/1144305529.2955.11.camel@corona
copy link · copy postgr.es
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: mike@middlesoft.co.uk, acoliver@jboss.org
Subject: Re: [PATCH] Support for ping method.
In-Reply-To: <1144305529.2955.11.camel@corona>
References: <1144305529.2955.11.camel@corona>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox