diff --git a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java index 20672f74b9d4..ee2a45cf5d93 100644 --- a/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java +++ b/instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/JdbcUtils.java @@ -65,7 +65,16 @@ public static Connection connectionFromStatement(Statement statement) { } public static DbInfo extractDbInfo(Connection connection) { - return JdbcData.connectionInfo.computeIfAbsent(connection, JdbcUtils::computeDbInfo); + // intentionally not using computeIfAbsent() since that would perform computeDbInfo() under a + // lock, and computeDbInfo() calls back to the application code via Connection.getMetaData() + // which could then result in a deadlock + // (e.g. https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/4188) + DbInfo dbInfo = JdbcData.connectionInfo.get(connection); + if (dbInfo == null) { + dbInfo = computeDbInfo(connection); + JdbcData.connectionInfo.put(connection, dbInfo); + } + return dbInfo; } public static DbInfo computeDbInfo(Connection connection) {