Skip to content

Commit c6cf6cb

Browse files
committed
more
1 parent 29ecd0d commit c6cf6cb

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/OpenTelemetryPreparedStatement.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,6 @@ public void setObject(int parameterIndex, Object x, SQLType targetSqlType) throw
430430

431431
@Override
432432
public long executeLargeUpdate() throws SQLException {
433-
return wrapCall(
434-
query,
435-
() -> {
436-
try {
437-
return delegate.executeLargeUpdate();
438-
} catch (UnsupportedOperationException ignored) {
439-
// Fallback for drivers that only implement executeUpdate
440-
return (long) delegate.executeUpdate();
441-
}
442-
});
433+
return wrapCall(query, delegate::executeLargeUpdate);
443434
}
444435
}

instrumentation/jdbc/library/src/main/java/io/opentelemetry/instrumentation/jdbc/internal/OpenTelemetryStatement.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.sql.Connection;
2929
import java.sql.ResultSet;
3030
import java.sql.SQLException;
31-
import java.sql.SQLFeatureNotSupportedException;
3231
import java.sql.SQLWarning;
3332
import java.sql.Statement;
3433
import java.util.ArrayList;
@@ -330,22 +329,7 @@ public long getLargeMaxRows() throws SQLException {
330329

331330
@Override
332331
public long[] executeLargeBatch() throws SQLException {
333-
return wrapBatchCall(
334-
() -> {
335-
try {
336-
return delegate.executeLargeBatch();
337-
} catch (UnsupportedOperationException
338-
| SQLFeatureNotSupportedException
339-
| AbstractMethodError ignored) {
340-
// Older drivers rely on the default executeLargeBatch implementation, which throws.
341-
int[] batchResult = delegate.executeBatch();
342-
long[] converted = new long[batchResult.length];
343-
for (int index = 0; index < batchResult.length; index++) {
344-
converted[index] = batchResult[index];
345-
}
346-
return converted;
347-
}
348-
});
332+
return wrapBatchCall(delegate::executeLargeBatch);
349333
}
350334

351335
@Override

instrumentation/jdbc/testing/src/main/java/io/opentelemetry/instrumentation/jdbc/testing/AbstractJdbcInstrumentationTest.java

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
2727
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER;
2828
import static java.util.Arrays.asList;
29+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2930
import static org.junit.jupiter.api.Assumptions.assumeTrue;
3031

3132
import com.google.common.collect.ImmutableMap;
@@ -880,15 +881,27 @@ void testPreparedStatementLargeUpdate(
880881
String table)
881882
throws SQLException {
882883
Connection connection = wrap(conn);
883-
testPreparedStatementUpdateImpl(
884-
system,
885-
connection,
886-
username,
887-
query,
888-
spanName,
889-
url,
890-
table,
891-
statement -> assertThat(statement.executeLargeUpdate()).isEqualTo(0));
884+
885+
// executeLargeUpdate was added in JDBC 4.2
886+
if (supportsJdbc42(connection)) {
887+
testPreparedStatementUpdateImpl(
888+
system,
889+
connection,
890+
username,
891+
query,
892+
spanName,
893+
url,
894+
table,
895+
statement -> assertThat(statement.executeLargeUpdate()).isEqualTo(0));
896+
} else {
897+
// Driver doesn't support JDBC 4.2, expect UnsupportedOperationException
898+
// This is the correct behavior - instrumentation should not change driver behavior
899+
String sql = connection.nativeSQL(query);
900+
PreparedStatement statement = connection.prepareStatement(sql);
901+
cleanup.deferCleanup(statement);
902+
assertThatThrownBy(statement::executeLargeUpdate)
903+
.isInstanceOf(UnsupportedOperationException.class);
904+
}
892905
}
893906

894907
void testPreparedStatementUpdateImpl(
@@ -1440,13 +1453,28 @@ void testLargeBatch(String system, Connection connection, String username, Strin
14401453
// derby and hsqldb used in this test don't support executeLargeBatch
14411454
assumeTrue("h2".equals(system));
14421455

1443-
testBatchImpl(
1444-
system,
1445-
wrap(connection),
1446-
username,
1447-
url,
1448-
"simple_batch_test_large",
1449-
statement -> assertThat(statement.executeLargeBatch()).isEqualTo(new long[] {1, 1}));
1456+
Statement createTable = wrap(connection).createStatement();
1457+
createTable.execute(
1458+
"CREATE TABLE simple_batch_test_large (id INTEGER not NULL, PRIMARY KEY ( id ))");
1459+
Statement statement = wrap(connection).createStatement();
1460+
statement.addBatch("INSERT INTO simple_batch_test_large VALUES(1)");
1461+
statement.addBatch("INSERT INTO simple_batch_test_large VALUES(2)");
1462+
1463+
// executeLargeBatch was added in JDBC 4.2
1464+
if (supportsJdbc42(connection)) {
1465+
assertThat(statement.executeLargeBatch()).isEqualTo(new long[] {1, 1});
1466+
} else {
1467+
// Driver doesn't support JDBC 4.2, expect UnsupportedOperationException
1468+
// This is the correct behavior - instrumentation should not change driver behavior
1469+
assertThatThrownBy(statement::executeLargeBatch)
1470+
.isInstanceOf(UnsupportedOperationException.class);
1471+
}
1472+
}
1473+
1474+
private static boolean supportsJdbc42(Connection connection) throws SQLException {
1475+
return connection.getMetaData().getJDBCMajorVersion() > 4
1476+
|| (connection.getMetaData().getJDBCMajorVersion() == 4
1477+
&& connection.getMetaData().getJDBCMinorVersion() >= 2);
14501478
}
14511479

14521480
private void testBatchImpl(

0 commit comments

Comments
 (0)