/** * Commits an active transaction of this instance of DBITransaction */ protected void commit(Handle handle) { try { handle.commit(); } catch (Exception e) { LOGGER.error("Failed to commit transaction", e); } }
@Override @SuppressWarnings("PMD.EmptyCatchBlock") public void close() throws IOException { try { super.close(); } finally { try { handle.commit(); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ex) { // ignore } handle.close(); } } }
@Override @SuppressWarnings("PMD.EmptyCatchBlock") public void close() throws IOException { try { super.close(); } finally { try { handle.commit(); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ex) { // ignore } handle.close(); } } }
public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.release("transaction#explicit"); h.getHandle().commit(); return null; } }
@Override public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.release("transaction#explicit"); h.getHandle().commit(); return null; } }
private void withTransaction(Consumer<Handle> cb) { try (Handle h = dbi.open()) { try { h.begin(); cb.accept(h); h.commit(); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException")RuntimeException e) { h.rollback(); throw e; } } }
private void withTransaction(Consumer<Handle> cb) { try (final Handle h = dbi.open()) { try { h.begin(); cb.accept(h); h.commit(); } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException")RuntimeException e) { h.rollback(); throw e; } } }
public void cleanup() throws SQLException { if (handle != null) { if (handle.isInTransaction()) { if (state == TransactionState.COMMIT) { handle.commit(); } else { handle.rollback(); } } handle.close(); } } };
@Test public void testLogBegin() throws Exception { h.begin(); assertTrue(logged.contains("begin")); h.commit(); }
@Test public void testLogBegin() throws Exception { h.begin(); assertTrue(logged.contains("begin")); h.commit(); }
@Test public void testLogCommit() throws Exception { h.begin(); h.commit(); assertTrue(logged.contains("commit")); }
@Test public void testLogCommit() throws Exception { h.begin(); h.commit(); assertTrue(logged.contains("commit")); }
// TODO: add try/catch/finally to close things properly DBI dbi = new DBI("jdbc:h2:mem:test"); Handle h = dbi.open(); h.begin(); Dao1 dao1 = h.attach(Dao1.class); Dao2 dao2 = h.attach(Dao2.class); dao1.save(myBean1); dao2.save(myBean2); h.commit(); h.close();
@Test public void testCheckpoint() throws Exception { h.begin(); h.checkpoint("a"); assertTrue(logged.contains("checkpoint a created")); h.rollback("a"); assertTrue(logged.contains("checkpoint a rolled back to")); h.checkpoint("b"); assertTrue(logged.contains("checkpoint b created")); h.release("b"); assertTrue(logged.contains("checkpoint b released")); h.commit(); } }
@Test public void testDoubleOpen() throws Exception { Handle h = openHandle(); assertTrue(h.getConnection().getAutoCommit()); h.begin(); h.begin(); assertFalse(h.getConnection().getAutoCommit()); h.commit(); assertTrue(h.getConnection().getAutoCommit()); }
@Test public void testDoubleOpen() throws Exception { Handle h = openHandle(); assertTrue(h.getConnection().getAutoCommit()); h.begin(); h.begin(); assertFalse(h.getConnection().getAutoCommit()); h.commit(); assertTrue(h.getConnection().getAutoCommit()); }
@Test public void testCheckpoint() throws Exception { Handle h = openHandle(); h.begin(); h.insert("insert into something (id, name) values (:id, :name)", 1, "Tom"); h.checkpoint("first"); h.insert("insert into something (id, name) values (:id, :name)", 1, "Martin"); assertEquals(Integer.valueOf(2), h.createQuery("select count(*) from something").mapTo(Integer.class).first()); h.rollback("first"); assertEquals(Integer.valueOf(1), h.createQuery("select count(*) from something").mapTo(Integer.class).first()); h.commit(); assertEquals(Integer.valueOf(1), h.createQuery("select count(*) from something").mapTo(Integer.class).first()); }
@Test public void testCheckpoint() throws Exception { Handle h = openHandle(); h.begin(); h.insert("insert into something (id, name) values (:id, :name)", 1, "Tom"); h.checkpoint("first"); h.insert("insert into something (id, name) values (:id, :name)", 1, "Martin"); assertEquals(Integer.valueOf(2), h.createQuery("select count(*) from something").map(new IntegerMapper()).first()); h.rollback("first"); assertEquals(Integer.valueOf(1), h.createQuery("select count(*) from something").map(new IntegerMapper()).first()); h.commit(); assertEquals(Integer.valueOf(1), h.createQuery("select count(*) from something").map(new IntegerMapper()).first()); }
@Test public void testJustJdbiTransactions() throws Exception { Handle h1 = dbi.open(); Handle h2 = dbi.open(); h1.execute("insert into something (id, name) values (8, 'Mike')"); h1.begin(); h1.execute("update something set name = 'Miker' where id = 8"); assertEquals("Mike", h2.createQuery("select name from something where id = 8").mapTo(String.class).first()); h1.commit(); h1.close(); h2.close(); }
@Test public void testJustJdbiTransactions() throws Exception { Handle h1 = dbi.open(); Handle h2 = dbi.open(); h1.execute("insert into something (id, name) values (8, 'Mike')"); h1.begin(); h1.execute("update something set name = 'Miker' where id = 8"); assertEquals("Mike", h2.createQuery("select name from something where id = 8").map(StringMapper.FIRST).first()); h1.commit(); h1.close(); h2.close(); }