Skip to content

Commit b9bd6fb

Browse files
committed
fix JDBC's SQLCommenter not taking into account semicolons
After adding the new always append comment feature in #9798, a customer found a bug that was present in our SQL Commenter since a long time. Basically, JDBC always split queries to send to the DB on the semicolon. If a comment is after a semicolon, JDBC will still split the queries, and return multiple ResultSet. This provoked exceptions like this one: ``` org.postgresql.util.PSQLException: Multiple ResultSets were returned by the query ``` SDBM-2100
1 parent 434b694 commit b9bd6fb

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/SQLCommenter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,14 @@ public static String inject(
103103

104104
StringBuilder sb = new StringBuilder(sql.length() + INJECTED_COMMENT_ESTIMATED_SIZE);
105105
if (appendComment) {
106-
sb.append(sql);
106+
if (sql.trim().endsWith(";")) {
107+
sb.append(sql, 0, sql.lastIndexOf(";"));
108+
} else {
109+
sb.append(sql);
110+
}
107111
sb.append(SPACE);
108112
}
113+
109114
sb.append(OPEN_COMMENT);
110115
int initSize = sb.length();
111116
append(sb, PARENT_SERVICE, config.getServiceName(), initSize);
@@ -128,6 +133,11 @@ public static String inject(
128133
sb.append(SPACE);
129134
sb.append(sql);
130135
}
136+
137+
if (appendComment && sql.trim().endsWith(";")) {
138+
sb.append(';');
139+
}
140+
131141
return sb.toString();
132142
}
133143

dd-java-agent/instrumentation/jdbc/src/test/groovy/SQLCommenterTest.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class SQLCommenterTest extends InstrumentationSpecification {
4747
where:
4848
query | ddService | ddEnv | dbService | dbType | host | dbName | ddVersion | append | traceParent | expected
4949
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
50+
"SELECT * FROM foo;" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/;"
51+
"SELECT * FROM foo; " | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/;"
52+
"SELECT * FROM foo; SELECT * FROM bar" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo; SELECT * FROM bar /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
53+
"SELECT * FROM foo; SELECT * FROM bar; " | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo; SELECT * FROM bar /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/;"
5054
"SELECT * FROM foo" | "SqlCommenter" | "Test" | "my-service" | "postgres" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "SELECT * FROM foo /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
5155
"{call dogshelterProc(?, ?)}" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | true | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "{call dogshelterProc(?, ?)} /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"
5256
"{call dogshelterProc(?, ?)}" | "SqlCommenter" | "Test" | "my-service" | "mysql" | "h" | "n" | "TestVersion" | false | "00-00000000000000007fffffffffffffff-000000024cb016ea-00" | "{call dogshelterProc(?, ?)} /*ddps='SqlCommenter',dddbs='my-service',ddh='h',dddb='n',dde='Test',ddpv='TestVersion',traceparent='00-00000000000000007fffffffffffffff-000000024cb016ea-00'*/"

0 commit comments

Comments
 (0)