diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java b/src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java index 435de47ac..ad36a1eaa 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java @@ -2154,6 +2154,26 @@ final void close() { packetLogger.finest(logMsg.toString()); } + + /** + * Get the current socket SO_TIMEOUT value. + * + * @return the current socket timeout value + * @throws IOException thrown if the socket timeout cannot be read + */ + final int getNetworkTimeout() throws IOException { + return tcpSocket.getSoTimeout(); + } + + /** + * Set the socket SO_TIMEOUT value. + * + * @param timeout the socket timeout in milliseconds + * @throws IOException thrown if the socket timeout cannot be set + */ + final void setNetworkTimeout(int timeout) throws IOException { + tcpSocket.setSoTimeout(timeout); + } } /** diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java index 700347dfa..54c5950c2 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java @@ -4636,14 +4636,42 @@ public void setHoldability(int holdability) throws SQLServerException { } public int getNetworkTimeout() throws SQLException { - // this operation is not supported - throw new SQLFeatureNotSupportedException(SQLServerException.getErrString("R_notSupported")); + loggerExternal.entering(getClassNameLogging(), "getNetworkTimeout"); + + checkClosed(); + + int timeout = 0; + try { + timeout = tdsChannel.getNetworkTimeout(); + } + catch (IOException ioe) { + terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, ioe.getMessage(), ioe); + } + + loggerExternal.exiting(getClassNameLogging(), "getNetworkTimeout"); + return timeout; } public void setNetworkTimeout(Executor executor, int timeout) throws SQLException { - // this operation is not supported - throw new SQLFeatureNotSupportedException(SQLServerException.getErrString("R_notSupported")); + loggerExternal.entering(getClassNameLogging(), "setNetworkTimeout", timeout); + + if (timeout < 0) { + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidSocketTimeout")); + Object[] msgArgs = {timeout}; + SQLServerException.makeFromDriverError(this, this, form.format(msgArgs), null, false); + } + + checkClosed(); + + try { + tdsChannel.setNetworkTimeout(timeout); + } + catch (IOException ioe) { + terminate(SQLServerException.DRIVER_ERROR_IO_FAILED, ioe.getMessage(), ioe); + } + + loggerExternal.exiting(getClassNameLogging(), "setNetworkTimeout"); } public String getSchema() throws SQLException { diff --git a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionPoolProxy.java b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionPoolProxy.java index b92e93d28..b180a11c9 100644 --- a/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionPoolProxy.java +++ b/src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnectionPoolProxy.java @@ -554,14 +554,14 @@ public PreparedStatement prepareStatement(String sql, } public int getNetworkTimeout() throws SQLException { - // The driver currently does not implement the optional JDBC APIs - throw new SQLFeatureNotSupportedException(SQLServerException.getErrString("R_notSupported")); + checkClosed(); + return wrappedConnection.getNetworkTimeout(); } public void setNetworkTimeout(Executor executor, int timeout) throws SQLException { - // The driver currently does not implement the optional JDBC APIs - throw new SQLFeatureNotSupportedException(SQLServerException.getErrString("R_notSupported")); + checkClosed(); + wrappedConnection.setNetworkTimeout(executor, timeout); } public String getSchema() throws SQLException {