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;
}
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);
}
}
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);
});
}
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"));
}
}
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));
});
}
Aggregations