@Test public void shouldCommitIfExecuteQuerySucceeds() throws Exception { when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); final JDBCBase.StatementWithReturnBlock<?> mockStatementWithReturnBlock = mock(JDBCBase.StatementWithReturnBlock.class); initializeJDBCTestSubclass(); jdbcTestSubclass.executeQuery("SOME SQL", mockStatementWithReturnBlock); InOrder inOrder = inOrder(mockPreparedStatement, mockConnection); inOrder.verify(mockConnection).commit(); inOrder.verify(mockPreparedStatement).close(); inOrder.verify(mockConnection).commit(); inOrder.verify(mockConnection, times(0)).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();  }