private void assertSuppressions(Throwable cause) { LoggerFactory.getLogger(TestBatchExceptionRewrite.class).info("exception", cause); SQLException e = (SQLException) cause; SQLException nextException = e.getNextException(); assertThat((Exception) nextException).isEqualTo(e.getSuppressed()[0]); assertThat((Exception) nextException.getNextException()).isNull(); assertThat(e.getSuppressed()).hasSize(1); } }
@Test public void testEventuallyFails() { final AtomicInteger attempts = new AtomicInteger(0); Handle handle = dbRule.getJdbi().open(); assertThatExceptionOfType(SQLException.class) .isThrownBy(() -> handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, conn -> { attempts.incrementAndGet(); throw new SQLException("serialization", "40001", attempts.get()); })) .satisfies(e -> assertThat(e.getSQLState()).isEqualTo("40001")) .satisfies(e -> assertThat(e.getSuppressed()) .hasSize(MAX_RETRIES) .describedAs("suppressed are ordered reverse chronologically, like a stack") .isSortedAccordingTo(Comparator.comparing(ex -> ((SQLException) ex).getErrorCode()).reversed())) .describedAs("thrown exception is chronologically last") .satisfies(e -> assertThat(e.getErrorCode()).isEqualTo(((SQLException) e.getSuppressed()[0]).getErrorCode() + 1)); assertThat(attempts.get()).isEqualTo(1 + MAX_RETRIES); }
.getNextException(); Throwable[] suppressed = e.getSuppressed(); for (int j = 0; j < suppressed.length; j++) { suppressed[j].printStackTrace();
.getNextException(); Throwable[] suppressed = e.getSuppressed(); for (int j = 0; j < suppressed.length; j++) { suppressed[j].printStackTrace();