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 for Classloader leak issue #465

Merged
merged 5 commits into from
Sep 20, 2017
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
29 changes: 19 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/ActivityCorrelator.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@

package com.microsoft.sqlserver.jdbc;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* ActivityCorrelator provides the APIs to access the ActivityId in TLS
*/
final class ActivityCorrelator {

private static ThreadLocal<ActivityId> ActivityIdTls = new ThreadLocal<ActivityId>() {
protected ActivityId initialValue() {
return new ActivityId();
private static Map<Long, ActivityId> ActivityIdTlsMap = new ConcurrentHashMap<Long, ActivityId>();

static void cleanupActivityId() {
//remove the ActivityId that belongs to this thread.
long uniqueThreadId = Thread.currentThread().getId();

if (ActivityIdTlsMap.containsKey(uniqueThreadId)) {
ActivityIdTlsMap.remove(uniqueThreadId);
}
};
}

// Get the current ActivityId in TLS
static ActivityId getCurrent() {
// get the value in TLS, not reference
return ActivityIdTls.get();
long uniqueThreadId = Thread.currentThread().getId();

//Since the Id for each thread is unique, this assures that the below if statement is run only once per thread.
if (!ActivityIdTlsMap.containsKey(uniqueThreadId)) {
ActivityIdTlsMap.put(uniqueThreadId, new ActivityId());
}

return ActivityIdTlsMap.get(uniqueThreadId);
}

// Increment the Sequence number of the ActivityId in TLS
// and return the ActivityId with new Sequence number
static ActivityId getNext() {
// We need to call get() method on ThreadLocal to get
// the current value of ActivityId stored in TLS,
// then increment the sequence number.

// Get the current ActivityId in TLS
ActivityId activityId = getCurrent();

Expand All @@ -47,7 +57,6 @@ static void setCurrentActivityIdSentFlag() {
ActivityId activityId = getCurrent();
activityId.setSentFlag();
}

}

class ActivityId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2992,6 +2992,8 @@ public void close() throws SQLServerException {

// Clean-up queue etc. related to batching of prepared statement discard actions (sp_unprepare).
cleanupPreparedStatementDiscardActions();

ActivityCorrelator.cleanupActivityId();

loggerExternal.exiting(getClassNameLogging(), "close");
}
Expand All @@ -3012,6 +3014,7 @@ final void poolCloseEventNotify() throws SQLServerException {
connectionCommand("IF @@TRANCOUNT > 0 ROLLBACK TRAN" /* +close connection */, "close connection");
}
notifyPooledConnection(null);
ActivityCorrelator.cleanupActivityId();
if (connectionlogger.isLoggable(Level.FINER)) {
connectionlogger.finer(toString() + " Connection closed and returned to connection pool");
}
Expand Down