@Test public void shouldRollbackCommitAndCloseConnectionIfExceptionIsThrownInExecute() throws Exception { final JDBCBase.ConnectionBlock mockConnectionBlock = mock(JDBCBase.ConnectionBlock.class); doThrow(IllegalArgumentException.class).when(mockConnectionBlock).use(mockConnection); initializeJDBCTestSubclass(); assertThatExceptionIsThrown(new Callable<Void>() { @Override public Void call() throws Exception { jdbcTestSubclass.execute(mockConnectionBlock); return null; } }, IllegalArgumentException.class); InOrder inOrder = inOrder(mockConnection); inOrder.verify(mockConnection).rollback(); inOrder.verify(mockConnection).close(); }
protected final void execute(ConnectionBlock connectionBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { connectionBlock.use(connection); connection.commit(); committed = true; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } }
JDBCBase { protected final void execute(ConnectionBlock connectionBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { connectionBlock.use(connection); connection.commit(); committed = true; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } }
JDBCBase { protected final void execute(ConnectionBlock connectionBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { connectionBlock.use(connection); connection.commit(); committed = true; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } protected  JDBCBase(String url, String username, @Nullable String password); }
JDBCBase { protected final void execute(ConnectionBlock connectionBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { connectionBlock.use(connection); connection.commit(); committed = true; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } protected  JDBCBase(String url, String username, @Nullable String password); synchronized final void initialize(); synchronized final void teardown(); }
JDBCBase { protected final void execute(ConnectionBlock connectionBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { connectionBlock.use(connection); connection.commit(); committed = true; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } protected  JDBCBase(String url, String username, @Nullable String password); synchronized final void initialize(); synchronized final void teardown();  }