简体   繁体   English

PostgreSQL JDBC ResultSet在单表扫描中具有预期行的4倍

[英]PostgreSQL JDBC ResultSet has 4x the expected rows on single table scan

I am running Tomcat 9.0 with a PostgreSQL 10.2 Database, using the Tomcat JDBC Pool configured with the org.postgresql.Driver. 我正在使用配置了org.postgresql.Driver的Tomcat JDBC Pool与PostgreSQL 10.2数据库一起运行Tomcat 9.0。

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. 该表正好有3行,并且无论使用DISTINCT关键字如何,pgAdmin查询工具都将返回相同的3行。

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). 我在下面的代码中使用SQL将值添加到列表中(当前不使用rank列,在我将putIfAbsent与地图一起使用以解决该错误之前)。

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. 我正在查看列表的JSON输出以及打印的消息。 While the table has 3 rows, I can see 12 entries. 该表有3行,但我可以看到12个条目。 They are ordered in non-distinct groups of 3, as if the query was repeated 4 times in order, returned to the same ResultSet. 它们以3个不明显的组的顺序排列,就好像该查询按顺序重复了4次一样,返回到相同的ResultSet。

Any idea what could be causing this and how to solve it? 知道是什么原因造成的,以及如何解决? Am I doing something wrong with my JDBC code? 我的JDBC代码有问题吗?

HoneyboyWilson found the problem. HoneyboyWilson发现了问题。 Was calling the wrong statement but it contained the correct columns, so was not producing any exceptions. 调用了错误的语句,但是它包含正确的列,因此没有产生任何异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM