DB.transact(new BatchInsertGenKeysOp.FjList<>( "whatevs", x -> ps -> {
@Test public void rollback2() { DB<Unit> failedInsert = insertKeysOp(list("ok")) .bind(ids -> insertDataOp(ids.zip(list("description"))).bind(insertCount -> { throw new RuntimeException("failed I have"); })); swallow(() -> DB.transact(failedInsert)); DB<P2<Long, Long>> tryCounts = COUNT_IDS.bind(idCount -> COUNT_DATA.map(dataCount -> p(idCount, dataCount))); P2<Long, Long> counts = DB.submit(tryCounts); assertEquals(0, (long) counts._1()); assertEquals(0, (long) counts._2()); }
@Test public void submitIsNotTransactional() { DB<Unit> fail = insertKeysOp(arrayList("a", "b", "c")).bind(ignore -> new DB<Unit>() { @Override public Unit run(Connection c) throws SQLException { throw new SQLException("failed i have"); } }); swallow(() -> DB.submit(fail)); assertThat(DB.submit(SELECT_ALL_IDS_OP), is(arrayList("a", "b", "c"))); assertThat(DB.transact(SELECT_ALL_IDS_OP), is(arrayList("a", "b", "c"))); }
public static DB<Unit> insertOrders(List<CreateOrder> newOrders) { // insert all the orders via batch, return the ORDER_ID keys DB<List<Integer>> insertOrdersGetKeys = new BatchInsertGenKeysOp.FjList<>( "INSERT INTO ORDERS(USER_EMAIL, TEXT) VALUES(?, ?)", x -> ps -> { ps.setString(1, x.userEmail); ps.setString(2, x.text); }, newOrders, rs -> rs.getInt(1) ); return insertOrdersGetKeys.map(ignore -> Unit.unit()); }
@Test public void selectBy() { DB<Unit> insertIt = insertKeysOp(list("a")) .bind(ids -> insertDataOp(ids.zip(list("my_description"))).map(ignore -> Unit.unit())); DB.submit(insertIt); List<String> descriptions = DB.submit(selectByDescOp("my_description")); assertEquals(list("my_description"), descriptions); }
@Test public void chainedTransaction() { DB<List<String>> readInserted = insertKeysOp(asList("a", "b", "c")).bind(ids -> { List<P2<Integer, String>> data = ids.zip(list("Pesho", "Gosho", "Dragan")); return insertDataOp(data).bind(whatever -> SELECT_ALL_DATA_OP); }); List<String> result = DB.transact(readInserted); assertThat(result, is(list("Pesho", "Gosho", "Dragan"))); }
@Test public void rollback() { DB<List<Long>> insertDataFail = insertKeysOp(list("okidoki")).bind(ids -> fail("failed i have")); swallow(() -> DB.transact(insertDataFail)); DB<P2<Long, Long>> tryCounts = COUNT_IDS.bind(idCount -> COUNT_DATA.map(dataCount -> p(idCount, dataCount))); P2<Long, Long> counts = DB.submit(tryCounts); assertEquals(0, (long) counts._1()); assertEquals(0, (long) counts._2()); }
private static BatchInsertGenKeysOp.FjList<String, Integer> insertKeysOp(Iterable<String> dummys) { return new BatchInsertGenKeysOp.FjList<>("INSERT INTO MySqlTest_IDS(DUMMY) VALUES (?)", x -> st -> st.setString(1, x), dummys, rs -> rs.getInt(1)); }