|
26 | 26 | import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM; |
27 | 27 | import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_USER; |
28 | 28 | import static java.util.Arrays.asList; |
| 29 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
29 | 30 | import static org.junit.jupiter.api.Assumptions.assumeTrue; |
30 | 31 |
|
31 | 32 | import com.google.common.collect.ImmutableMap; |
@@ -880,15 +881,27 @@ void testPreparedStatementLargeUpdate( |
880 | 881 | String table) |
881 | 882 | throws SQLException { |
882 | 883 | 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 | + } |
892 | 905 | } |
893 | 906 |
|
894 | 907 | void testPreparedStatementUpdateImpl( |
@@ -1440,13 +1453,28 @@ void testLargeBatch(String system, Connection connection, String username, Strin |
1440 | 1453 | // derby and hsqldb used in this test don't support executeLargeBatch |
1441 | 1454 | assumeTrue("h2".equals(system)); |
1442 | 1455 |
|
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); |
1450 | 1478 | } |
1451 | 1479 |
|
1452 | 1480 | private void testBatchImpl( |
|
0 commit comments