Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket timeout implementation for both connection string and data source #85

Merged
merged 2 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ final void open(
// Set socket options
tcpSocket.setTcpNoDelay(true);
tcpSocket.setKeepAlive(true);

//set SO_TIMEOUT
int socketTimeout = con.getSocketTimeoutMilliseconds();
tcpSocket.setSoTimeout(socketTimeout);

inputStream = tcpInputStream = tcpSocket.getInputStream();
outputStream = tcpOutputStream = tcpSocket.getOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ final ApplicationIntent getApplicationIntent()
final String getResponseBuffering() { return responseBuffering; }
private int queryTimeoutSeconds ;
final int getQueryTimeoutSeconds() { return queryTimeoutSeconds; }
private int socketTimeoutMilliseconds ;
final int getSocketTimeoutMilliseconds() { return socketTimeoutMilliseconds; }

private boolean sendTimeAsDatetime = SQLServerDriverBooleanProperty.SEND_TIME_AS_DATETIME.getDefaultValue();

Expand Down Expand Up @@ -1435,15 +1437,15 @@ else if (0 == requestedPacketSize)
}

sPropKey = SQLServerDriverIntProperty.LOCK_TIMEOUT.toString();
int defaultTimeOut = SQLServerDriverIntProperty.LOCK_TIMEOUT.getDefaultValue();
nLockTimeout = defaultTimeOut; //Wait forever
int defaultLockTimeOut = SQLServerDriverIntProperty.LOCK_TIMEOUT.getDefaultValue();
nLockTimeout = defaultLockTimeOut; //Wait forever
if (activeConnectionProperties.getProperty(sPropKey) != null &&
activeConnectionProperties.getProperty(sPropKey).length() > 0)
{
try
{
int n = (new Integer(activeConnectionProperties.getProperty(sPropKey))).intValue();
if (n>=defaultTimeOut)
if (n>=defaultLockTimeOut)
nLockTimeout = n;
else
{
Expand All @@ -1469,7 +1471,7 @@ else if (0 == requestedPacketSize)
try
{
int n = (new Integer(activeConnectionProperties.getProperty(sPropKey))).intValue();
if (n>=defaultTimeOut){
if (n>=defaultQueryTimeout){
queryTimeoutSeconds = n;
}
else
Expand All @@ -1486,6 +1488,33 @@ else if (0 == requestedPacketSize)
SQLServerException.makeFromDriverError(this, this, form.format(msgArgs), null, false);
}
}

sPropKey = SQLServerDriverIntProperty.SOCKET_TIMEOUT.toString();
int defaultSocketTimeout = SQLServerDriverIntProperty.SOCKET_TIMEOUT.getDefaultValue();
socketTimeoutMilliseconds = defaultSocketTimeout; //Wait forever
if (activeConnectionProperties.getProperty(sPropKey) != null &&
activeConnectionProperties.getProperty(sPropKey).length() > 0)
{
try
{
int n = (new Integer(activeConnectionProperties.getProperty(sPropKey))).intValue();
if (n>=defaultSocketTimeout){
socketTimeoutMilliseconds = n;
}
else
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidSocketTimeout"));
Object[] msgArgs = {activeConnectionProperties.getProperty(sPropKey)};
SQLServerException.makeFromDriverError(this, this, form.format(msgArgs), null, false);
}
}
catch (NumberFormatException e)
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidSocketTimeout"));
Object[] msgArgs = {activeConnectionProperties.getProperty(sPropKey)};
SQLServerException.makeFromDriverError(this, this, form.format(msgArgs), null, false);
}
}

FailoverInfo fo =null;
String databaseNameProperty = SQLServerDriverStringProperty.DATABASE_NAME.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,16 @@ public int getPacketSize()
{
return getIntProperty(connectionProps, SQLServerDriverIntProperty.PACKET_SIZE.toString(), SQLServerDriverIntProperty.PACKET_SIZE.getDefaultValue());
}

public void setSocketTimeout(int socketTimeout)
{
setIntProperty(connectionProps, SQLServerDriverIntProperty.SOCKET_TIMEOUT.toString(), socketTimeout);
}
public int getSocketTimeout()
{
int defaultTimeOut = SQLServerDriverIntProperty.SOCKET_TIMEOUT.getDefaultValue();
return getIntProperty(connectionProps, SQLServerDriverIntProperty.SOCKET_TIMEOUT.toString(), defaultTimeOut);
}

// responseBuffering controls the driver's buffering of responses from SQL Server.
// Possible values are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,9 @@ enum SQLServerDriverIntProperty
PACKET_SIZE("packetSize", TDS.DEFAULT_PACKET_SIZE),
LOCK_TIMEOUT("lockTimeout", -1),
LOGIN_TIMEOUT("loginTimeout", 15),
QUERY_TIMEOUT("queryTimeout", -1),
PORT_NUMBER("portNumber", 1433);
QUERY_TIMEOUT("queryTimeout", -1),
PORT_NUMBER("portNumber", 1433),
SOCKET_TIMEOUT("socketTimeout", 0);

private String name;
private int defaultValue;
Expand Down Expand Up @@ -381,6 +382,7 @@ public final class SQLServerDriver implements java.sql.Driver
new SQLServerDriverPropertyInfo(SQLServerDriverBooleanProperty.XOPEN_STATES.toString(), Boolean.toString(SQLServerDriverBooleanProperty.XOPEN_STATES.getDefaultValue()), false, TRUE_FALSE),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.AUTHENTICATION_SCHEME.toString(), SQLServerDriverStringProperty.AUTHENTICATION_SCHEME.getDefaultValue(), false, new String[] {AuthenticationScheme.javaKerberos.toString(),AuthenticationScheme.nativeAuthentication.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverStringProperty.AUTHENTICATION.toString(), SQLServerDriverStringProperty.AUTHENTICATION.getDefaultValue(), false, new String[] {SqlAuthentication.NotSpecified.toString(),SqlAuthentication.SqlPassword.toString(),SqlAuthentication.ActiveDirectoryPassword.toString(),SqlAuthentication.ActiveDirectoryIntegrated.toString()}),
new SQLServerDriverPropertyInfo(SQLServerDriverIntProperty.SOCKET_TIMEOUT.toString(), Integer.toString(SQLServerDriverIntProperty.SOCKET_TIMEOUT.getDefaultValue()), false, null),
};

//Properties that can only be set by using Properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ protected Object[][] getContents()
{"R_sendTimeAsDatetimePropertyDescription", "Determines whether to use the SQL Server datetime data type to send java.sql.Time values to the database."},
{"R_TransparentNetworkIPResolutionPropertyDescription", "Determines whether to use the Transparent Network IP Resolution feature."},
{"R_queryTimeoutPropertyDescription", "The number of seconds to wait before the database reports a query time-out."},
{"R_socketTimeoutPropertyDescription", "The number of milliseconds to wait before the java.net.SocketTimeoutException is raised."},
{"R_noParserSupport", "An error occurred while instantiating the required parser. Error: \"{0}\""},
{"R_writeOnlyXML", "Cannot read from this SQLXML instance. This instance is for writing data only."},
{"R_dataHasBeenReadXML", "Cannot read from this SQLXML instance. The data has already been read."},
Expand Down Expand Up @@ -382,6 +383,7 @@ protected Object[][] getContents()
{"R_invalidServerCursorForTVP" , "Use different Connection for source ResultSet and prepared query, if selectMethod is set to cursor for Table-Valued Parameter."},
{"R_TVPnotWorkWithSetObjectResultSet" , "setObject() with ResultSet is not supported for Table-Valued Parameter. Please use setStructured()"},
{"R_invalidQueryTimeout", "The queryTimeout {0} is not valid."},
{"R_invalidSocketTimeout", "The socketTimeout {0} is not valid."},
};
}