Search in sources :

Example 6 with Jdbi

use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.

the class JdbiUtil method getHandle.

/**
 * Obtain a Handle instance, either the transactionally bound one if we are in a transaction,
 * or a new one otherwise.
 * @param jdbi the Jdbi instance from which to obtain the handle
 *
 * @return the Handle instance
 */
public static Handle getHandle(Jdbi jdbi) {
    Handle bound = (Handle) TransactionSynchronizationManager.getResource(jdbi);
    if (bound == null) {
        bound = jdbi.open();
        if (TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.bindResource(jdbi, bound);
            TransactionSynchronizationManager.registerSynchronization(new Adapter(jdbi, bound));
            TRANSACTIONAL_HANDLES.add(bound);
        }
    }
    return bound;
}
Also used : TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Handle(org.jdbi.v3.core.Handle)

Example 7 with Jdbi

use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.

the class TestJdbiFactoryBean method testFailsViaException.

@Test
public void testFailsViaException() throws Exception {
    assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
        service.inPropagationRequired(jdbi -> {
            Handle h = JdbiUtil.getHandle(jdbi);
            final int count = h.execute("insert into something (id, name) values (7, 'ignored')");
            if (count == 1) {
                throw new ForceRollback();
            } else {
                throw new RuntimeException("!ZABAK");
            }
        });
    });
    try (final Handle h = Jdbi.open(ds)) {
        int count = h.createQuery("select count(*) from something").mapTo(int.class).findOnly();
        assertThat(count).isEqualTo(0);
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 8 with Jdbi

use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.

the class TestJdbiFactoryBean method testNested.

@Test
public void testNested() throws Exception {
    assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
        service.inPropagationRequired(outer -> {
            final Handle h = JdbiUtil.getHandle(outer);
            h.execute("insert into something (id, name) values (7, 'ignored')");
            assertThatExceptionOfType(ForceRollback.class).isThrownBy(() -> {
                service.inNested(inner -> {
                    final Handle h1 = JdbiUtil.getHandle(inner);
                    h1.execute("insert into something (id, name) values (8, 'ignored again')");
                    int count = h1.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
                    assertThat(count).isEqualTo(2);
                    throw new ForceRollback();
                });
            });
            int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
            assertThat(count).isEqualTo(1);
            throw new ForceRollback();
        });
    });
    service.inPropagationRequired(jdbi -> {
        final Handle h = JdbiUtil.getHandle(jdbi);
        int count = h.createQuery("select count(*) from something").mapTo(Integer.class).findOnly();
        assertThat(count).isEqualTo(0);
    });
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 9 with Jdbi

use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.

the class TestPreparedBatchGenerateKeys method testBatchInsertWithKeyGeneration.

@Test
public void testBatchInsertWithKeyGeneration() throws Exception {
    Jdbi db = Jdbi.create("jdbc:hsqldb:mem:jdbi-batch-keys-test", "sa", "");
    try (Handle h = db.open()) {
        h.execute("create table something (id integer not null generated by default as identity (start with 10000), name varchar(50) )");
        PreparedBatch batch = h.prepareBatch("insert into something (name) values (?)");
        batch.add("Brian");
        batch.add("Thom");
        List<Integer> ids = batch.executeAndReturnGeneratedKeys().mapTo(int.class).list();
        assertThat(ids).containsExactly(10000, 10001);
        List<Something> somethings = h.createQuery("select id, name from something").mapToBean(Something.class).list();
        assertThat(somethings).containsExactly(new Something(10000, "Brian"), new Something(10001, "Thom"));
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 10 with Jdbi

use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.

the class TestBindList method testBindListWithHashPrefixParser.

@Test
public void testBindListWithHashPrefixParser() throws Exception {
    Jdbi jdbi = Jdbi.create(dbRule.getConnectionFactory());
    jdbi.setSqlParser(new HashPrefixSqlParser());
    jdbi.useHandle(handle -> {
        handle.registerRowMapper(FieldMapper.factory(Thing.class));
        handle.createUpdate("insert into thing (<columns>) values (<values>)").defineList("columns", "id", "foo").bindList("values", 3, "abc").execute();
        List<Thing> list = handle.createQuery("select id, foo from thing where id in (<ids>)").bindList("ids", 1, 3).mapTo(Thing.class).list();
        assertThat(list).extracting(Thing::getId, Thing::getFoo, Thing::getBar, Thing::getBaz).containsExactly(tuple(1, "foo1", null, null), tuple(3, "abc", null, null));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)36 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)11 Something (org.jdbi.v3.core.Something)6 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Query (org.jdbi.v3.core.statement.Query)5 Before (org.junit.Before)5 SQLException (java.sql.SQLException)4 Update (org.jdbi.v3.core.statement.Update)3 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)3 BeforeClass (org.junit.BeforeClass)3 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)2 DefaultStatementBuilder (org.jdbi.v3.core.statement.DefaultStatementBuilder)2 SqlObjects (org.jdbi.v3.sqlobject.SqlObjects)2 Bind (org.jdbi.v3.sqlobject.customizer.Bind)2 SqlQuery (org.jdbi.v3.sqlobject.statement.SqlQuery)2 Rule (org.junit.Rule)2