@Test public void shouldRollbackCommitAndCloseConnectionIfExceptionIsThrownInExecuteQuery() throws Exception { when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); final JDBCBase.StatementWithReturnBlock<?> mockStatementWithReturnBlock = mock(JDBCBase.StatementWithReturnBlock.class); doThrow(IllegalArgumentException.class).when(mockStatementWithReturnBlock).use(mockPreparedStatement); initializeJDBCTestSubclass(); assertThatExceptionIsThrown(new Callable<Void>() { @Override public Void call() throws Exception { jdbcTestSubclass.executeQuery("NO SQL", mockStatementWithReturnBlock); return null; } }, IllegalArgumentException.class); InOrder inOrder = inOrder(mockPreparedStatement, mockConnection); inOrder.verify(mockPreparedStatement).close(); inOrder.verify(mockConnection).rollback(); inOrder.verify(mockConnection).close(); }
protected final <T> T executeQuery(final String sql, final StatementWithReturnBlock<T> queryBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { T returnValue = withStatement(connection, sql, queryBlock); connection.commit(); committed = true; return returnValue; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } }
JDBCBase { protected final <T> T executeQuery(final String sql, final StatementWithReturnBlock<T> queryBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { T returnValue = withStatement(connection, sql, queryBlock); connection.commit(); committed = true; return returnValue; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } }
JDBCBase { protected final <T> T executeQuery(final String sql, final StatementWithReturnBlock<T> queryBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { T returnValue = withStatement(connection, sql, queryBlock); connection.commit(); committed = true; return returnValue; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } protected  JDBCBase(String url, String username, @Nullable String password); }
JDBCBase { protected final <T> T executeQuery(final String sql, final StatementWithReturnBlock<T> queryBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { T returnValue = withStatement(connection, sql, queryBlock); connection.commit(); committed = true; return returnValue; } 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 <T> T executeQuery(final String sql, final StatementWithReturnBlock<T> queryBlock) throws Exception { checkState(initialized); setupConnection(); checkNotNull(connection); boolean committed = false; try { T returnValue = withStatement(connection, sql, queryBlock); connection.commit(); committed = true; return returnValue; } finally { if (!committed) { connection.rollback(); cleanupConnection(); } } } protected  JDBCBase(String url, String username, @Nullable String password); synchronized final void initialize(); synchronized final void teardown();  }