@Override public void abort() { if (handle == null) { return; } if (state == State.COMMITTED) { throw new IllegalStateException("Aborting committed transaction is not allowed"); } if (!autoAutoCommit) { handle.rollback(); } state = State.ABORTED; }
/** * Rollback an active transaction of this instance of DBITransaction */ protected void rollback(Handle handle) { if (isNull(handle)) return; try { handle.rollback(); } catch (Exception e) { LOGGER.error("Failed rolling back transaction", e); } }
public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.getHandle().rollback(String.valueOf(args[0])); return null; } }
public void rollback() { if (transactionHandle == null) { LOG.warn("Got a late rollback after commit/rollback. Ignoring!"); } else { transactionHandle.rollback(); transactionHandle.close(); transactionHandle = null; } }
@Override public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.getHandle().rollback(String.valueOf(args[0])); return null; } }
public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.release("transaction#explicit"); h.getHandle().rollback(); return null; } }
@Override public Object invoke(HandleDing h, Object target, Object[] args, MethodProxy mp) { h.release("transaction#explicit"); h.getHandle().rollback(); 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(); } } };
@Override 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 testLogRollback() throws Exception { h.begin(); h.rollback(); assertTrue(logged.contains("rollback")); }
@Test public void testLogRollback() throws Exception { h.begin(); h.rollback(); assertTrue(logged.contains("rollback")); }
@Test public void testRollbackOutsideTx() throws Exception { Handle h = openHandle(); h.insert("insert into something (id, name) values (?, ?)", 7, "Tom"); h.rollback(); }
@Test public void testRollbackOutsideTx() throws Exception { Handle h = openHandle(); h.insert("insert into something (id, name) values (?, ?)", 7, "Tom"); h.rollback(); }
@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 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 testReleaseCheckpoint() throws Exception { Handle h = openHandle(); h.begin(); h.checkpoint("first"); h.insert("insert into something (id, name) values (:id, :name)", 1, "Martin"); h.release("first"); try { h.rollback("first"); fail("Should have thrown an exception of some kind"); } catch (TransactionException e) { h.rollback(); assertTrue(true); } }
@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()); }