postgresql-jdbc.git / summary / log / commit / refs
commit 32f2e5079aa6ac2ced08d4afd399eabb916ee349
Author: Kris Jurka <books@ejurka.com>
Date: Sun Apr 13 16:03:50 2008 +0000
Add a configuration option to enable tcp keep alives.
Toru SHIMOGAKI
doc/pgjdbc.xml | 12 +++++++++++-
org/postgresql/Driver.java.in | 4 +++-
org/postgresql/core/v2/ConnectionFactoryImpl.java | 8 +++++++-
org/postgresql/core/v3/ConnectionFactoryImpl.java | 8 +++++++-
org/postgresql/ds/common/BaseDataSource.java | 18 +++++++++++++++++-
5 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/doc/pgjdbc.xml b/doc/pgjdbc.xml
index 70c823ec..24eb2275 100644
--- a/doc/pgjdbc.xml
+++ b/doc/pgjdbc.xml
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!--
Copyright (c) 2004-2008, PostgreSQL Global Development Group
- $PostgreSQL: pgjdbc/doc/pgjdbc.xml,v 1.32 2007/07/27 09:10:54 jurka Exp $
+ $PostgreSQL: pgjdbc/doc/pgjdbc.xml,v 1.33 2008/01/08 06:56:26 jurka Exp $
-->
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
@@ -591,6 +591,16 @@ Connection conn = DriverManager.getConnection(url);
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><varname>tcpKeepAlive</varname> = <type>boolean</type></term>
+ <listitem>
+ <para>
+ Enable or disable TCP keep-alive probe. The default is
+ <literal>false</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><varname>stringtype</varname> = <type>String</type></term>
<listitem>
diff --git a/org/postgresql/Driver.java.in b/org/postgresql/Driver.java.in
index d8e0ee5b..82334eff 100644
--- a/org/postgresql/Driver.java.in
+++ b/org/postgresql/Driver.java.in
@@ -3,7 +3,7 @@
* Copyright (c) 2003-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/Driver.java.in,v 1.71 2007/11/02 15:27:44 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/Driver.java.in,v 1.72 2008/01/08 06:56:26 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -442,6 +442,8 @@ public class Driver implements java.sql.Driver
new String[] { "7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2" } },
{ "loginTimeout", Boolean.FALSE,
"The login timeout, in seconds; 0 means no timeout beyond the normal TCP connection timout." },
+ { "tcpKeepAlive", Boolean.FALSE,
+ "Enable or disable TCP keep-alive probe." },
{ "stringtype", Boolean.FALSE,
"The type to bind String parameters as (usually 'varchar'; 'unspecified' allows implicit casting to other types)",
new String[] { "varchar", "unspecified" } },
diff --git a/org/postgresql/core/v2/ConnectionFactoryImpl.java b/org/postgresql/core/v2/ConnectionFactoryImpl.java
index 6ed2e90f..fc88d187 100644
--- a/org/postgresql/core/v2/ConnectionFactoryImpl.java
+++ b/org/postgresql/core/v2/ConnectionFactoryImpl.java
@@ -4,7 +4,7 @@
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v 1.13 2007/10/08 01:49:59 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v 1.14 2008/01/08 06:56:27 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -46,6 +46,9 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
boolean requireSSL = (info.getProperty("ssl") != null);
boolean trySSL = requireSSL; // XXX temporary until we revisit the ssl property values
+ // - the TCP keep alive setting
+ boolean requireTCPKeepAlive = (Boolean.valueOf(info.getProperty("tcpKeepAlive")).booleanValue());
+
if (logger.logDebug())
logger.debug("Trying to establish a protocol version 2 connection to " + host + ":" + port);
@@ -69,6 +72,9 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
if (trySSL)
newStream = enableSSL(newStream, requireSSL, info, logger);
+ // Enable TCP keep-alive probe if required.
+ newStream.getSocket().setKeepAlive(requireTCPKeepAlive);
+
// Construct and send a startup packet.
sendStartupPacket(newStream, user, database, logger);
diff --git a/org/postgresql/core/v3/ConnectionFactoryImpl.java b/org/postgresql/core/v3/ConnectionFactoryImpl.java
index 5840910c..ec804830 100644
--- a/org/postgresql/core/v3/ConnectionFactoryImpl.java
+++ b/org/postgresql/core/v3/ConnectionFactoryImpl.java
@@ -4,7 +4,7 @@
* Copyright (c) 2004, Open Cloud Limited.
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.14 2007/10/08 01:49:59 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.15 2008/01/08 06:56:27 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,6 +50,9 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
boolean requireSSL = (info.getProperty("ssl") != null);
boolean trySSL = requireSSL; // XXX temporary until we revisit the ssl property values
+ // - the TCP keep alive setting
+ boolean requireTCPKeepAlive = (Boolean.valueOf(info.getProperty("tcpKeepAlive")).booleanValue());
+
// NOTE: To simplify this code, it is assumed that if we are
// using the V3 protocol, then the database is at least 7.4. That
// eliminates the need to check database versions and maintain
@@ -80,6 +83,9 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
if (trySSL)
newStream = enableSSL(newStream, requireSSL, info, logger);
+ // Enable TCP keep-alive probe if required.
+ newStream.getSocket().setKeepAlive(requireTCPKeepAlive);
+
// Construct and send a startup packet.
String[][] params = {
{ "user", user },
diff --git a/org/postgresql/ds/common/BaseDataSource.java b/org/postgresql/ds/common/BaseDataSource.java
index d3e7a390..741d4332 100644
--- a/org/postgresql/ds/common/BaseDataSource.java
+++ b/org/postgresql/ds/common/BaseDataSource.java
@@ -3,7 +3,7 @@
* Copyright (c) 2004-2008, PostgreSQL Global Development Group
*
* IDENTIFICATION
-* $PostgreSQL: pgjdbc/org/postgresql/ds/common/BaseDataSource.java,v 1.10 2008/01/08 06:56:27 jurka Exp $
+* $PostgreSQL: pgjdbc/org/postgresql/ds/common/BaseDataSource.java,v 1.11 2008/04/13 15:49:16 jurka Exp $
*
*-------------------------------------------------------------------------
*/
@@ -51,6 +51,7 @@ public abstract class BaseDataSource implements Referenceable
private int loginTimeout; // in seconds
private boolean ssl = false;
private String sslfactory;
+ private boolean tcpKeepAlive = false;
/**
* Gets a connection to the PostgreSQL database. The database is identified by the
@@ -303,6 +304,16 @@ public abstract class BaseDataSource implements Referenceable
return this.sslfactory;
}
+ public void setTcpKeepAlive(boolean enabled)
+ {
+ tcpKeepAlive = enabled;
+ }
+
+ public boolean getTcpKeepAlive()
+ {
+ return tcpKeepAlive;
+ }
+
/**
* Generates a DriverManager URL from the other properties supplied.
*/
@@ -323,6 +334,7 @@ public abstract class BaseDataSource implements Referenceable
sb.append("&sslfactory=").append(sslfactory);
}
}
+ sb.append("&tcpkeepalive=").append(tcpKeepAlive);
return sb.toString();
}
@@ -361,6 +373,8 @@ public abstract class BaseDataSource implements Referenceable
ref.add(new StringRefAddr("ssl", Boolean.toString(ssl)));
ref.add(new StringRefAddr("sslfactory", sslfactory));
+ ref.add(new StringRefAddr("tcpKeepAlive", Boolean.toString(tcpKeepAlive));
+
return ref;
}
@@ -375,6 +389,7 @@ public abstract class BaseDataSource implements Referenceable
out.writeInt(loginTimeout);
out.writeBoolean(ssl);
out.writeObject(sslfactory);
+ out.writeBoolean(tcpKeepAlive);
}
protected void readBaseObject(ObjectInputStream in) throws IOException, ClassNotFoundException
@@ -388,6 +403,7 @@ public abstract class BaseDataSource implements Referenceable
loginTimeout = in.readInt();
ssl = in.readBoolean();
sslfactory = (String)in.readObject();
+ tcpKeepAlive = in.readBoolean();
}
}
[parent: 7c82342bb8c0]