Skip to content

Commit

Permalink
Adding a clear to see if this fixes prior errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffery-Wasty committed Aug 8, 2024
1 parent 4d782f2 commit f741762
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,81 @@ public void testSqlServerBulkCopyCachingConnectionLevel() throws Exception {
}
}

@Test
public void testSqlServerBulkCopyCachingConnectionLevelMultiThreaded() throws Exception {
Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long ms = 1578743412000L;
long timeOut = 90000; // 90 seconds
int NUMBER_SIMULTANEOUS_INSERTS = 40;

try (SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(connectionString
+ ";useBulkCopyForBatchInsert=true;cacheBulkCopyMetadata=true;sendTemporalDataTypesAsStringForBulkCopy=false;");
Statement stmt = con.createStatement()) {

// Needs to be on a JDK version greater than 8
assumeTrue(TestUtils.getJVMVersion() > 8);

TestUtils.dropTableIfExists(timestampTable1, stmt);
String createSqlTable1 = "CREATE TABLE " + timestampTable1 + " (c1 DATETIME2(3))";
stmt.execute(createSqlTable1);

Field bulkcopyMetadataCacheField;

if (con.getClass().getName().equals("com.microsoft.sqlserver.jdbc.SQLServerConnection43")) {
bulkcopyMetadataCacheField = con.getClass().getSuperclass()
.getDeclaredField("BULK_COPY_OPERATION_CACHE");
} else {
bulkcopyMetadataCacheField = con.getClass().getDeclaredField("BULK_COPY_OPERATION_CACHE");
}

bulkcopyMetadataCacheField.setAccessible(true);
Object bulkcopyCache = bulkcopyMetadataCacheField.get(con);

((HashMap<?, ?>) bulkcopyCache).clear();

TimerTask task = new TimerTask() {
public void run() {
((HashMap<?, ?>) bulkcopyCache).clear();
fail(TestResource.getResource("R_executionTooLong"));
}
};
Timer timer = new Timer("Timer");
timer.schedule(task, timeOut); // Run a timer to help us exit if we get deadlocked

final CountDownLatch countDownLatch = new CountDownLatch(NUMBER_SIMULTANEOUS_INSERTS);
Runnable runnable = () -> {
try {
for (int i = 0; i < 5; ++i) {
PreparedStatement preparedStatement = con
.prepareStatement("INSERT INTO " + timestampTable1 + " VALUES(?)");
Timestamp timestamp = new Timestamp(ms);
preparedStatement.setTimestamp(1, timestamp, gmtCal);
preparedStatement.addBatch();
preparedStatement.executeBatch();
}
countDownLatch.countDown();
countDownLatch.await();
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
} finally {
((HashMap<?, ?>) bulkcopyCache).clear();
}
};

ExecutorService executor = Executors.newFixedThreadPool(NUMBER_SIMULTANEOUS_INSERTS);

try {
for (int i = 0; i < NUMBER_SIMULTANEOUS_INSERTS; i++) {
executor.submit(runnable);
}
executor.shutdown();
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedException") + e.getMessage());
} finally {
((HashMap<?, ?>) bulkcopyCache).clear();
}
}
}

@Test
public void testValidTimezoneForTimestampBatchInsertWithBulkCopy() throws Exception {
Expand Down

0 comments on commit f741762

Please sign in to comment.