use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testBeanPropertyNestedBinding.
@Test
public void testBeanPropertyNestedBinding() throws Exception {
Handle h = dbRule.openHandle();
Something thing = new Something(0, "Keith");
assertThat(h.createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)").bindBean("my", new Object() {
@SuppressWarnings("unused")
public Something getNested() {
return thing;
}
}).execute()).isEqualTo(1);
assertThat(h.select("select * from something where id = ?", thing.getId()).mapToBean(Something.class).findOnly()).isEqualTo(thing);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testInsert.
@Test
public void testInsert() throws Exception {
Handle h = dbRule.openHandle();
Update insert = h.createUpdate("insert into something (id, name) values (:id, :name)");
insert.bind("id", 1);
insert.bind("name", "Brian");
int count = insert.execute();
assertThat(count).isEqualTo(1);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class FiveMinuteTourTest method setUp.
@Before
public void setUp() {
// tag::createJdbi[]
// H2 in-memory database
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
// end::createJdbi[]
// shared handle to keep database open
this.jdbi = jdbi;
this.handle = jdbi.open();
// tag::useHandle[]
jdbi.useHandle(handle -> {
handle.execute("create table contacts (id int primary key, name varchar(100))");
handle.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
handle.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
});
// end::useHandle[]
// tag::withHandle[]
List<String> names = jdbi.withHandle(handle -> handle.createQuery("select name from contacts").mapTo(String.class).list());
assertThat(names).contains("Alice", "Bob");
// end::withHandle[]
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class IntroductionTest method core.
@Test
public void core() {
// tag::core[]
// (H2 in-memory database)
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
List<User> users = jdbi.withHandle(handle -> {
handle.execute("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)");
// Inline positional parameters
handle.execute("INSERT INTO user(id, name) VALUES (?, ?)", 0, "Alice");
// Positional parameters
handle.createUpdate("INSERT INTO user(id, name) VALUES (?, ?)").bind(0, // 0-based parameter indexes
1).bind(1, "Bob").execute();
// Named parameters
handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bind("id", 2).bind("name", "Clarice").execute();
// Named parameters from bean properties
handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bindBean(new User(3, "David")).execute();
// Easy mapping to any type
return handle.createQuery("SELECT * FROM user ORDER BY name").mapToBean(User.class).list();
});
assertThat(users).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::core[]
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TransactionTest method serializableTransaction.
@Test
public void serializableTransaction() throws Exception {
// tag::serializable[]
// Automatically rerun transactions
db.setTransactionHandler(new SerializableTransactionRunner());
// Set up some values
BiConsumer<Handle, Integer> insert = (h, i) -> h.execute("INSERT INTO ints(value) VALUES(?)", i);
handle.execute("CREATE TABLE ints (value INTEGER)");
insert.accept(handle, 10);
insert.accept(handle, 20);
// Run the following twice in parallel, and synchronize
ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(2);
Callable<Integer> sumAndInsert = () -> db.inTransaction(TransactionIsolationLevel.SERIALIZABLE, h -> {
// Both read initial state of table
int sum = h.select("SELECT sum(value) FROM ints").mapTo(int.class).findOnly();
// First time through, make sure neither transaction writes until both have read
latch.countDown();
latch.await();
// Now do the write.
insert.accept(h, sum);
return sum;
});
// Both of these would calculate 10 + 20 = 30, but that violates serialization!
Future<Integer> result1 = executor.submit(sumAndInsert);
Future<Integer> result2 = executor.submit(sumAndInsert);
// One of the transactions gets 30, the other will abort and automatically rerun.
// On the second attempt it will compute 10 + 20 + 30 = 60, seeing the update from its sibling.
// This assertion fails under any isolation level below SERIALIZABLE!
assertThat(result1.get() + result2.get()).isEqualTo(30 + 60);
executor.shutdown();
// end::serializable[]
}
Aggregations