Skip to content

Commit 93ecbc6

Browse files
Merge pull request #7411 from DataDog/nenadnoveljic/sqlserver-improve-exception-handling
Improve exception handling in full mode for SQL Server
2 parents 0e0a715 + 88369e8 commit 93ecbc6

1 file changed

Lines changed: 8 additions & 12 deletions

File tree

  • dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ public long setContextInfo(Connection connection, DBInfo dbInfo) {
267267
AgentTracer.get().buildSpan("set context_info").withTag("dd.instrumentation", true).start();
268268
DECORATE.afterStart(instrumentationSpan);
269269
DECORATE.onConnection(instrumentationSpan, dbInfo);
270-
PreparedStatement instrumentationStatement = null;
271270
try (AgentScope scope = activateSpan(instrumentationSpan)) {
272271
final byte samplingDecision =
273272
(byte) (instrumentationSpan.forceSamplingDecision() > 0 ? 1 : 0);
@@ -283,12 +282,15 @@ public long setContextInfo(Connection connection, DBInfo dbInfo) {
283282
byteBuffer.putLong(traceId.toHighOrderLong());
284283
byteBuffer.putLong(traceId.toLong());
285284
final byte[] contextInfo = byteBuffer.array();
286-
287285
String instrumentationSql = "set context_info ?";
288-
instrumentationStatement = connection.prepareStatement(instrumentationSql);
289-
instrumentationStatement.setBytes(1, contextInfo);
290-
DECORATE.onStatement(instrumentationSpan, instrumentationSql);
291-
instrumentationStatement.execute();
286+
try (PreparedStatement instrumentationStatement =
287+
connection.prepareStatement(instrumentationSql)) {
288+
instrumentationStatement.setBytes(1, contextInfo);
289+
DECORATE.onStatement(instrumentationSpan, instrumentationSql);
290+
instrumentationStatement.execute();
291+
} catch (SQLException e) {
292+
throw e;
293+
}
292294
} catch (Exception e) {
293295
log.debug(
294296
"Failed to set extra DBM data in context info for trace {}. "
@@ -298,12 +300,6 @@ public long setContextInfo(Connection connection, DBInfo dbInfo) {
298300
e);
299301
DECORATE.onError(instrumentationSpan, e);
300302
} finally {
301-
if (instrumentationStatement != null) {
302-
try {
303-
instrumentationStatement.close();
304-
} catch (Throwable e) {
305-
}
306-
}
307303
instrumentationSpan.finish();
308304
}
309305
return spanID;

0 commit comments

Comments
 (0)