Skip to content

Commit

Permalink
Fix | Fix SQLServerConnection.abort() API behavior to clear resources…
Browse files Browse the repository at this point in the history
… consistently (#983)
  • Loading branch information
cheenamalhotra authored Mar 13, 2019
1 parent 4e4d6bc commit 408b614
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3169,16 +3169,10 @@ public void rollback() throws SQLServerException {
public void abort(Executor executor) throws SQLException {
loggerExternal.entering(getClassNameLogging(), "abort", executor);

// nop if connection is closed
// no-op if connection is closed
if (isClosed())
return;

if (null == executor) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidArgument"));
Object[] msgArgs = {"executor"};
SQLServerException.makeFromDriverError(null, null, form.format(msgArgs), null, false);
}

// check for callAbort permission
SecurityManager secMgr = System.getSecurityManager();
if (secMgr != null) {
Expand All @@ -3191,11 +3185,20 @@ public void abort(Executor executor) throws SQLException {
SQLServerException.makeFromDriverError(this, this, form.format(msgArgs), null, true);
}
}
if (null == executor) {
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_invalidArgument"));
Object[] msgArgs = {"executor"};
SQLServerException.makeFromDriverError(null, null, form.format(msgArgs), null, false);
} else {

setState(State.Closed);
/*
* Always report the connection as closed for any further use, no matter what happens when we try to clean
* up the physical resources associated with the connection using executor.
*/
setState(State.Closed);

if (null != tdsChannel && null != executor)
executor.execute(() -> tdsChannel.close());
executor.execute(() -> clearConnectionResources());
}

loggerExternal.exiting(getClassNameLogging(), "abort");
}
Expand All @@ -3204,19 +3207,27 @@ public void abort(Executor executor) throws SQLException {
public void close() throws SQLServerException {
loggerExternal.entering(getClassNameLogging(), "close");

// Always report the connection as closed for any further use, no matter
// what happens when we try to clean up the physical resources associated
// with the connection.
/*
* Always report the connection as closed for any further use, no matter what happens when we try to clean up
* the physical resources associated with the connection.
*/
setState(State.Closed);

clearConnectionResources();

loggerExternal.exiting(getClassNameLogging(), "close");
}

private void clearConnectionResources() {
if (sharedTimer != null) {
sharedTimer.removeRef();
sharedTimer = null;
}

// Close the TDS channel. When the channel is closed, the server automatically
// rolls back any pending transactions and closes associated resources like
// prepared handles.
/*
* Close the TDS channel. When the channel is closed, the server automatically rolls back any pending
* transactions and closes associated resources like prepared handles.
*/
if (null != tdsChannel) {
tdsChannel.close();
}
Expand All @@ -3232,8 +3243,6 @@ public void close() throws SQLServerException {
cleanupPreparedStatementDiscardActions();

ActivityCorrelator.cleanupActivityId();

loggerExternal.exiting(getClassNameLogging(), "close");
}

// This function is used by the proxy for notifying the pool manager that this connection proxy is closed
Expand Down

0 comments on commit 408b614

Please sign in to comment.