/** * Submits this operation for execution in the executor service. The operation is executed with connection autoCommit = true, * i.e. non-transactionally. */ public <A> Future<A> submit(DB<A> op) { return withConnection(op, true); }
private <A> Future<A> withConnection(DB<A> op, boolean autoCommit) { return ex.submit(() -> { try (Connection c = getConnection(autoCommit)) { return op.run(c); } }); }
@Test public void sanity() { Future<Integer> success = dbAsync.submit(DB.unit(42)); SQLException ex = new SQLException("failed i have"); Future<Integer> fail = dbAsync.submit(DB.db((Try1<Connection, Integer, SQLException>) c -> { assertThat(c, is(notNullValue())); throw ex; })); assertThat(awaitAndGet(success), is(42)); assertThat(awaitAndGetFailure(fail), is(ex)); }
@BeforeClass public static void setupHikari() { ds = Hikari.createHikari("jdbc:hsqldb:mem:JdbcUtilsTest", "sa", "", new Properties()); ex = Hikari.createExecutorFor(ds, false, () -> new UnorderedThreadPoolEventExecutor(0, new DefaultThreadFactory("sanity-test")) ); dbAsync = new FutureInterpreter(ds, SanityTest.ex); }
/** * Submits this operation for execution in the executor service. The operation is executed as a transaction. */ public <A> Future<A> transact(DB<A> op) { return withConnection(transactional(op), false); }