Skip to content

Commit 179ba92

Browse files
authored
Setting JDBC statement map cleaner thread ccl to null (#799)
1 parent ebc7b57 commit 179ba92

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
This makes the runtime attachment work in more environments such as minimal Docker containers.
1414
Note that the runtime attachment currently does not work for OSGi containers like those used in many application servers such as JBoss and WildFly.
1515
See the [documentation](https://www.elastic.co/guide/en/apm/agent/java/master/setup-attach-cli.html) for more information.
16+
* JDBC statement map is leaking in Tomcat if the application that first used it is udeployed/redeployed. See [this
17+
related discussion](https://discuss.elastic.co/t/elastic-apm-agent-jdbchelper-seems-to-use-a-lot-of-memory/195295).
1618

1719
# Breaking Changes
1820
* The `apm-agent-attach.jar` is not executable anymore.

apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/JdbcHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@
3535
public abstract class JdbcHelper {
3636
@SuppressWarnings("WeakerAccess")
3737
@VisibleForAdvice
38-
public static final WeakConcurrentMap<Object, String> statementSqlMap = new WeakConcurrentMap<>(true);
38+
public static final WeakConcurrentMap<Object, String> statementSqlMap;
39+
40+
static {
41+
statementSqlMap = new WeakConcurrentMap<>(true);
42+
// This class will probably be loaded in the context of a request processing thread
43+
// whose context class loader is the web application ClassLoader.
44+
// This leaks the web application ClassLoader when the application is undeployed/redeployed.
45+
// Tomcat will then stop the thread because it thinks it was created by the web application.
46+
// That means that the map will never be cleared, creating a severe memory leak.
47+
statementSqlMap.getCleanerThread().setContextClassLoader(null);
48+
}
3949

4050
/**
4151
* Maps the provided sql to the provided Statement object

0 commit comments

Comments
 (0)