Permalink
Browse files

Fixes #638 Implement support for get/setNetworkTimeout() (#849)

  • Loading branch information...
brettwooldridge authored and davecramer committed Nov 23, 2017
1 parent befea18 commit 8a30044d9e97c1038ee4401ae745d37a11f008db
@@ -541,4 +541,12 @@ public void close() throws IOException {
pg_input.close();
connection.close();
}
public void setNetworkTimeout(int milliseconds) throws IOException {
connection.setSoTimeout(milliseconds);
}
public int getNetworkTimeout() throws IOException {
return connection.getSoTimeout();
}
}
@@ -14,6 +14,7 @@
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.util.HostSpec;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.List;
@@ -437,4 +438,8 @@ Object createQueryKey(String sql, boolean escapeProcessing, boolean isParameteri
* @return the ReplicationProtocol instance for this connection.
*/
ReplicationProtocol getReplicationProtocol();
void setNetworkTimeout(int milliseconds) throws IOException;
int getNetworkTimeout() throws IOException;
}
@@ -79,6 +79,16 @@ public void evict(CachedQuery cachedQuery) throws SQLException {
protected abstract void sendCloseMessage() throws IOException;
@Override
public void setNetworkTimeout(int milliseconds) throws IOException {
pgStream.setNetworkTimeout(milliseconds);
}
@Override
public int getNetworkTimeout() throws IOException {
return pgStream.getNetworkTimeout();
}
@Override
public HostSpec getHostSpec() {
return pgStream.getHostSpec();
@@ -79,6 +79,7 @@
private static final Logger LOGGER = Logger.getLogger(PgConnection.class.getName());
private static final SQLPermission SQL_PERMISSION_ABORT = new SQLPermission("callAbort");
private static final SQLPermission SQL_PERMISSION_NETWORK_TIMEOUT = new SQLPermission("setNetworkTimeout");
//
// Data initialized on construction:
@@ -1480,12 +1481,36 @@ public void abort(Executor executor) throws SQLException {
}
}
public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(), "setNetworkTimeout(Executor, int)");
public void setNetworkTimeout(Executor executor /*not used*/, int milliseconds) throws SQLException {
checkClosed();
if (milliseconds < 0) {
throw new PSQLException(GT.tr("Network timeout must be a value greater than or equal to 0."),
PSQLState.INVALID_PARAMETER_VALUE);
}
SecurityManager securityManager = System.getSecurityManager();
if (securityManager != null) {
securityManager.checkPermission(SQL_PERMISSION_NETWORK_TIMEOUT);
}
try {
queryExecutor.setNetworkTimeout(milliseconds);
} catch (IOException ioe) {
throw new PSQLException(GT.tr("Unable to set network timeout."),
PSQLState.COMMUNICATION_ERROR, ioe);
}
}
public int getNetworkTimeout() throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(), "getNetworkTimeout()");
checkClosed();
try {
return queryExecutor.getNetworkTimeout();
} catch (IOException ioe) {
throw new PSQLException(GT.tr("Unable to get network timeout."),
PSQLState.COMMUNICATION_ERROR, ioe);
}
}
@Override
@@ -17,6 +17,7 @@
AbortTest.class,
CloseOnCompletionTest.class,
SharedTimerClassLoaderLeakTest.class,
NetworkTimeoutTest.class,
GetObjectTest.class}
)
public class Jdbc41TestSuite {
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2007, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package org.postgresql.test.jdbc4.jdbc41;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.test.TestUtil;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
public class NetworkTimeoutTest {
@Test
public void testSetNetworkTimeout() throws Exception {
Connection _conn = TestUtil.openDB();
try {
_conn.setNetworkTimeout(null, 0);
} catch (SQLException e) {
fail("Connection.setNetworkTimeout() throw exception");
} finally {
TestUtil.closeDB(_conn);
}
}
@Test
public void testSetNetworkTimeoutInvalid() throws Exception {
Connection _conn = TestUtil.openDB();
try {
_conn.setNetworkTimeout(null, -1);
fail("Connection.setNetworkTimeout() did not throw expected exception");
} catch (SQLException e) {
// Passed
} finally {
TestUtil.closeDB(_conn);
}
}
@Test
public void testSetNetworkTimeoutValid() throws Exception {
Connection _conn = TestUtil.openDB();
try {
_conn.setNetworkTimeout(null, (int) TimeUnit.SECONDS.toMillis(5));
assertEquals(TimeUnit.SECONDS.toMillis(5), _conn.getNetworkTimeout());
} catch (SQLException e) {
fail("Connection.setNetworkTimeout() throw exception");
} finally {
TestUtil.closeDB(_conn);
}
}
@Test
public void testSetNetworkTimeoutEnforcement() throws Exception {
Connection _conn = TestUtil.openDB();
Statement stmt = null;
try {
_conn.setNetworkTimeout(null, (int) TimeUnit.SECONDS.toMillis(1));
stmt = _conn.createStatement();
stmt.execute("SELECT pg_sleep(2)");
fail("Connection.setNetworkTimeout() did not throw expected exception");
} catch (SQLException e) {
// assertTrue(stmt.isClosed());
assertTrue(_conn.isClosed());
} finally {
TestUtil.closeQuietly(stmt);
TestUtil.closeDB(_conn);
}
}
}

0 comments on commit 8a30044

Please sign in to comment.