use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestSerializableTransactionRunner method testEventuallySucceeds.
@Test
public void testEventuallySucceeds() throws Exception {
final AtomicInteger tries = new AtomicInteger(3);
Handle handle = db.open();
handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, conn -> {
if (tries.decrementAndGet() == 0) {
return null;
}
throw new SQLException("serialization", "40001");
});
assertThat(tries.get()).isZero();
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestSerializableTransactionRunner method testEventuallyFails.
@Test
public void testEventuallyFails() throws Exception {
final AtomicInteger tries = new AtomicInteger(5);
Handle handle = db.open();
assertThatExceptionOfType(SQLException.class).isThrownBy(() -> handle.inTransaction(TransactionIsolationLevel.SERIALIZABLE, conn -> {
tries.decrementAndGet();
throw new SQLException("serialization", "40001");
})).satisfies(e -> assertThat(e.getSQLState()).isEqualTo("40001"));
assertThat(tries.get()).isEqualTo(0);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestTransactionsAutoCommit method restoreAutoCommitInitialStateOnUnexpectedError.
@Test
public void restoreAutoCommitInitialStateOnUnexpectedError() throws Exception {
final Connection connection = mock(Connection.class);
final PreparedStatement statement = mock(PreparedStatement.class);
InOrder inOrder = inOrder(connection, statement);
Handle h = Jdbi.create(() -> connection).open();
when(connection.getAutoCommit()).thenReturn(true);
when(connection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(statement);
when(statement.execute()).thenReturn(true);
when(statement.getUpdateCount()).thenReturn(1);
// throw e.g some underlying database error
doThrow(new SQLException("infrastructure error")).when(connection).commit();
h.begin();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
h.execute(SAMPLE_SQL, 1L, "Tom");
// throws exception on commit
h.commit();
});
// expected behaviour chain:
// 1. store initial auto-commit state
inOrder.verify(connection, atLeastOnce()).getAutoCommit();
// 2. turn off auto-commit
inOrder.verify(connection).setAutoCommit(false);
// 3. execute statement (without commit)
inOrder.verify(connection).prepareStatement("insert into something (id, name) values (?, ?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
inOrder.verify(statement).execute();
inOrder.verify(statement).getUpdateCount();
// 4. commit transaction
inOrder.verify(connection).commit();
// 5. set auto-commit back to initial state
inOrder.verify(connection).setAutoCommit(true);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class CallTest method testCall.
@Test
public void testCall() {
Handle handle = db.getHandle();
handle.execute(findSqlOnClasspath("create_stored_proc_add"));
// tag::invokeProcedure[]
OutParameters result = handle.createCall(// <1>
"{:sum = call add(:a, :b)}").bind("a", // <2>
13).bind("b", // <2>
9).registerOutParameter("sum", // <3> <4>
Types.INTEGER).invoke();
// end::invokeProcedure[]
// tag::getOutParameters[]
int sum = result.getInt("sum");
// end::getOutParameters[]
assertThat(sum).isEqualTo(22);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class FieldMapperTest method testNestedPrefix.
@Test
public void testNestedPrefix() {
Handle handle = dbRule.getSharedHandle();
handle.execute("insert into something (id, name) values (1, 'foo')");
assertThat(handle.registerRowMapper(FieldMapper.factory(NestedPrefixThing.class)).select("select id nested_id, name nested_name from something").mapTo(NestedPrefixThing.class).findOnly()).extracting("nested.i", "nested.s").containsExactly(1, "foo");
}
Aggregations