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

Fix | Fix SQLServerConnection.abort() API behavior to clear resources consistently #983

Merged
merged 4 commits into from
Mar 13, 2019
Merged
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
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