PgConnection extends AbstractJdbcConnection { @Override public void executeReadOnlyQuery(String sql, Consumer<JdbcResultSet> resultHandler) throws NotReadOnlyException { try { execute("SET TRANSACTION READ ONLY"); try (Statement stmt = connection.createStatement()) { ResultSet rs = stmt.executeQuery(sql); resultHandler.accept(new PgResultSet(rs)); } execute("SET TRANSACTION READ WRITE"); } catch (SQLException ex) { if (ex.getSQLState().equals("25006")) { throw new NotReadOnlyException(ex); } else { throw new DatabaseException("Failed to execute given SELECT statement", ex); } } } protected  PgConnection(Connection connection); @VisibleForTesting static PgConnection open(PgConnectionConfig config); @Override void executeReadOnlyQuery(String sql, Consumer<JdbcResultSet> resultHandler); @Override String escapeIdent(String ident); @Override TransactionHelper getStrictTransactionHelper(String statusTableSchema, String statusTableName, Duration cleanupDuration);  }
@Test public void executeReadOnlyQuery() throws IOException, NotReadOnlyException, SQLException { AtomicReference<JdbcResultSet> rs = new AtomicReference<>(); pgConnection.executeReadOnlyQuery(SQL, rs::set); verify(pgConnection).execute(eq("SET TRANSACTION READ ONLY")); verify(statement).executeQuery(eq(SQL)); verify(pgConnection).execute(eq("SET TRANSACTION READ WRITE")); assertThat(rs.get(), is(notNullValue())); }