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 ClassLoader leak of ActivityCorrelator ThreadLocal #2366

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 23 additions & 7 deletions src/main/java/com/microsoft/sqlserver/jdbc/ActivityCorrelator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,29 @@

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
*/
lilgreenbird marked this conversation as resolved.
Show resolved Hide resolved
final class ActivityCorrelator {

private static ThreadLocal<ActivityId> t_ActivityId = new ThreadLocal<ActivityId>() {
@Override
protected ActivityId initialValue() {
return new ActivityId();
}
};
private static Map<Long, ActivityId> activityIdMap = new ConcurrentHashMap<Long, ActivityId>();

static ActivityId getCurrent() {
return t_ActivityId.get();
// get the value in TLS, not reference
@SuppressWarnings("deprecation")
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 (!activityIdMap.containsKey(uniqueThreadId)) {
activityIdMap.put(uniqueThreadId, new ActivityId());
}

return activityIdMap.get(uniqueThreadId);
}

// Increment the Sequence number of the ActivityId in TLS
Expand All @@ -34,6 +40,16 @@ static ActivityId getNext() {
* Prevent instantiation.
*/
private ActivityCorrelator() {}

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

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4785,6 +4785,8 @@ private void clearConnectionResources() {

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

ActivityCorrelator.cleanupActivityId();
}

/**
Expand All @@ -4806,6 +4808,8 @@ final void poolCloseEventNotify() throws SQLServerException {
}
notifyPooledConnection(null);

ActivityCorrelator.cleanupActivityId();

if (connectionlogger.isLoggable(Level.FINER)) {
connectionlogger.finer(toString() + " Connection closed and returned to connection pool");
}
Expand Down
Loading