Search in sources :

Example 51 with Handle

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQLException(java.sql.SQLException) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 52 with Handle

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SQLException(java.sql.SQLException) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 53 with Handle

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);
}
Also used : InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 54 with Handle

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);
}
Also used : OutParameters(org.jdbi.v3.core.statement.OutParameters) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 55 with Handle

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");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)123 Handle (org.jdbi.v3.core.Handle)119 Jdbi (org.jdbi.v3.core.Jdbi)31 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)20 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 SQLException (java.sql.SQLException)8 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Query (org.jdbi.v3.core.statement.Query)6 Rule (org.junit.Rule)5 Map (java.util.Map)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Method (java.lang.reflect.Method)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3