I am running Tomcat 9.0 with a PostgreSQL 10.2 Database, using the Tomcat JDBC Pool configured with the org.postgresql.Driver.
I have the following simple query, which scans a small reference table for values:
SELECT DISTINCT quality_rank, quality_desc
FROM flooring_quality_type
ORDER BY quality_rank
The table has exactly 3 rows, and the pgAdmin Query Tool returns those same 3 rows regardless of the DISTINCT keyword being used.
I am using the SQL in the following code to add the values to a list (currently not using the rank column, before I was using putIfAbsent with a map to get around the bug).
String QUALITIES_SQL = "SELECT DISTINCT quality_rank, quality_desc " +
" FROM flooring_quality_type ORDER BY quality_rank";
try (Connection con = DataSourceKeeper.getConnection();
PreparedStatement mapStmt = con.prepareStatement(PRICE_MAP_SQL); // Another query, not exhibiting problem
PreparedStatement qualStmt = con.prepareStatement(QUALITIES_SQL)) {
try (ResultSet qualRs = mapStmt.executeQuery()) {
while (qualRs.next()) {
System.out.println("Got another result");
qualities.add(qualRs.getString("quality_desc"));
}
}
// Other query executed here ... SNIP
// The mapStmt does not cause the same issue, but uses joins.
// Problem occurs regardless of the order of statement execution.
} catch (SQLException e) {
throw new UserFacingException();
}
I am looking at JSON output of the list, as well as the printed message. While the table has 3 rows, I can see 12 entries. They are ordered in non-distinct groups of 3, as if the query was repeated 4 times in order, returned to the same ResultSet.
Any idea what could be causing this and how to solve it? Am I doing something wrong with my JDBC code?