簡體   English   中英

PostgreSQL JDBC ResultSet在單表掃描中具有預期行的4倍

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

我正在使用配置了org.postgresql.Driver的Tomcat JDBC Pool與PostgreSQL 10.2數據庫一起運行Tomcat 9.0。

我有以下簡單查詢,該查詢會掃描小的參考表中的值:

SELECT DISTINCT quality_rank, quality_desc 
  FROM flooring_quality_type 
ORDER BY quality_rank

該表正好有3行,並且無論使用DISTINCT關鍵字如何,pgAdmin查詢工具都將返回相同的3行。

我在下面的代碼中使用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();
}

我正在查看列表的JSON輸出以及打印的消息。 該表有3行,但我可以看到12個條目。 它們以3個不明顯的組的順序排列,就好像該查詢按順序重復了4次一樣,返回到相同的ResultSet。

知道是什么原因造成的,以及如何解決? 我的JDBC代碼有問題嗎?

HoneyboyWilson發現了問題。 調用了錯誤的語句,但是它包含正確的列,因此沒有產生任何異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM