postgresql-jdbc.git / summary / log / commit / refs

commit    4e7f65b91e95d6249c78de6944f09cd34ef1ae87
Author:   Dave Cramer <dave.cramer@credativ.ca>
Date:     Wed Sep 12 00:55:41 2012 +0000

    patch from Bernd Helme for send recev buffer sizes


org/postgresql/core/v3/ConnectionFactoryImpl.java | 39 ++++++++++++++++++ org/postgresql/ds/common/BaseDataSource.java | 30 ++++++++++++++ org/postgresql/test/TestUtil.java | 26 +++++++++++- .../test/jdbc3/SendRecvBufferSizeTest.java | 48 ++++++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) diff --git a/org/postgresql/core/v3/ConnectionFactoryImpl.java b/org/postgresql/core/v3/ConnectionFactoryImpl.java index 03cacbac..d50c07da 100644 --- a/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -118,6 +118,45 @@ public class ConnectionFactoryImpl extends ConnectionFactory { // Enable TCP keep-alive probe if required. newStream.getSocket().setKeepAlive(requireTCPKeepAlive); + // Try to set SO_SNDBUF and SO_RECVBUF socket options, if requested. + // If receiveBufferSize and send_buffer_size are set to a value greater + // than 0, adjust. -1 means use the system default, 0 is ignored since not + // supported. + + // Set SO_RECVBUF read buffer size + String receiveBufferSizeProperty = info.getProperty("receiveBufferSize", "-1"); + try { + int receiveBufferSize = Integer.parseInt(receiveBufferSizeProperty); + if (receiveBufferSize > -1) { + // value of 0 not a valid buffer size value + if (receiveBufferSize > 0) { + newStream.getSocket().setReceiveBufferSize(receiveBufferSize); + } else { + logger.info("Ignore invalid value for receiveBufferSize: " + receiveBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse receiveBufferSize value: " + receiveBufferSizeProperty); + } + + // Set SO_SNDBUF write buffer size + String sendBufferSizeProperty = info.getProperty("sendBufferSize", "-1"); + try { + int sendBufferSize = Integer.parseInt(sendBufferSizeProperty); + if (sendBufferSize > -1) { + if (sendBufferSize > 0) { + newStream.getSocket().setSendBufferSize(sendBufferSize); + } else { + logger.info("Ignore invalid value for sendBufferSize: " + sendBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse sendBufferSize value: " + sendBufferSizeProperty); + } + + logger.info("Receive Buffer Size is " + newStream.getSocket().getReceiveBufferSize()); + logger.info("Send Buffer Size is " + newStream.getSocket().getSendBufferSize()); + // 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 a5891535..bfe54f7a 100644 --- a/org/postgresql/ds/common/BaseDataSource.java +++ b/org/postgresql/ds/common/BaseDataSource.java @@ -54,6 +54,8 @@ public abstract class BaseDataSource implements Referenceable private String binaryTransferDisable = ""; private int loginTimeout = 0; // in seconds private int socketTimeout = 0; // in seconds + private int receiveBufferSize = -1; // off (-1), not in use + private int sendBufferSize = -1; // off (-1), not in use private boolean ssl = false; private String sslfactory; private boolean tcpKeepAlive = false; @@ -292,6 +294,22 @@ public abstract class BaseDataSource implements Referenceable this.prepareThreshold = count; } + /** + * Sets the write buffer size of TCP/IP socket. + */ + public void setReceiveBufferSize(int nbytes) + { + this.receiveBufferSize = nbytes; + } + + /** + * Sets the send buffer size of TCP/IP socket. + */ + public void setSendBufferSize(int nbytes) + { + this.sendBufferSize = nbytes; + } + /** * Gets the default threshold for enabling server-side prepare. * @@ -478,6 +496,12 @@ public abstract class BaseDataSource implements Referenceable sb.append("&sslfactory=").append(sslfactory); } } + if (receiveBufferSize != -1) { + sb.append("&receiveBufferSize=").append(receiveBufferSize); + } + if (sendBufferSize != -1) { + sb.append("&sendBufferSize=").append(sendBufferSize); + } sb.append("&tcpkeepalive=").append(tcpKeepAlive); if (compatible != null) { sb.append("&compatible="+compatible); @@ -530,6 +554,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("receiveBufferSize", Integer.toString(receiveBufferSize))); + ref.add(new StringRefAddr("sendBufferSize", Integer.toString(sendBufferSize))); ref.add(new StringRefAddr("tcpKeepAlive", Boolean.toString(tcpKeepAlive))); if (compatible != null) { @@ -556,6 +582,8 @@ public abstract class BaseDataSource implements Referenceable out.writeInt(socketTimeout); out.writeBoolean(ssl); out.writeObject(sslfactory); + out.writeInt(receiveBufferSize); + out.writeInt(sendBufferSize); out.writeBoolean(tcpKeepAlive); out.writeObject(compatible); out.writeInt(logLevel); @@ -579,6 +607,8 @@ public abstract class BaseDataSource implements Referenceable socketTimeout = in.readInt(); ssl = in.readBoolean(); sslfactory = (String)in.readObject(); + receiveBufferSize = in.readInt(); + sendBufferSize = in.readInt(); tcpKeepAlive = in.readBoolean(); compatible = (String)in.readObject(); logLevel = in.readInt(); diff --git a/org/postgresql/test/TestUtil.java b/org/postgresql/test/TestUtil.java index 45fa7480..7c6c6121 100644 --- a/org/postgresql/test/TestUtil.java +++ b/org/postgresql/test/TestUtil.java @@ -35,6 +35,16 @@ public class TestUtil binaryTransfer = "&binaryTransfer=" + getBinaryTransfer(); } + String receiveBufferSize = ""; + if (getReceiveBufferSize() != -1 ){ + receiveBufferSize = "&receiveBufferSize="+getReceiveBufferSize(); + } + + String sendBufferSize = ""; + if (getSendBufferSize() != -1 ){ + sendBufferSize = "&sendBufferSize="+getSendBufferSize(); + } + return "jdbc:postgresql://" + getServer() + ":" + getPort() + "/" @@ -42,7 +52,9 @@ public class TestUtil + "?prepareThreshold=" + getPrepareThreshold() + "&loglevel=" + getLogLevel() + protocolVersion - + binaryTransfer; + + binaryTransfer + + receiveBufferSize + + sendBufferSize; } /* @@ -113,7 +125,17 @@ public class TestUtil { return System.getProperty("binaryTransfer"); } - + + public static int getSendBufferSize() + { + return Integer.parseInt(System.getProperty("sendBufferSize", "-1")); + } + + public static int getReceiveBufferSize() + { + return Integer.parseInt(System.getProperty("receiveBufferSize","-1")); + } + private static boolean initialized = false; public static void initDriver() throws Exception { diff --git a/org/postgresql/test/jdbc3/SendRecvBufferSizeTest.java b/org/postgresql/test/jdbc3/SendRecvBufferSizeTest.java new file mode 100644 index 00000000..54c51a5a --- /dev/null +++ b/org/postgresql/test/jdbc3/SendRecvBufferSizeTest.java @@ -0,0 +1,48 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2004-2011, PostgreSQL Global Development Group +* +* +*------------------------------------------------------------------------- +*/ +package org.postgresql.test.jdbc3; + +import java.sql.*; +import junit.framework.TestCase; +import org.postgresql.test.TestUtil; + +public class SendRecvBufferSizeTest extends TestCase { + + private Connection _conn; + + public SendRecvBufferSizeTest(String name) { + super(name); + } + + protected void setUp() throws Exception { + System.setProperty("sendBufferSize", "1024"); + System.setProperty("receiveBufferSize","1024"); + + _conn = TestUtil.openDB(); + Statement stmt = _conn.createStatement(); + stmt.execute("CREATE TEMP TABLE hold(a int)"); + stmt.execute("INSERT INTO hold VALUES (1)"); + stmt.execute("INSERT INTO hold VALUES (2)"); + stmt.close(); + } + + protected void tearDown() throws SQLException { + Statement stmt = _conn.createStatement(); + stmt.execute("DROP TABLE hold"); + stmt.close(); + TestUtil.closeDB(_conn); + } + + // dummy test + public void testSelect() throws SQLException { + Statement stmt = _conn.createStatement(); + stmt.execute("select * from hold"); + stmt.close(); + } + +} \ No newline at end of file [parent: f4903eafbc2a]