更新时间:2025-09-04 GMT+08:00

常见问题

  1. 现象:设置Statement、PreparedStatement的fetchSize为Integer.MIN_VALUE时出现如下异常:
    org.postgresql.util.PSQLException: Fetch size must be a value greater to or equal to 0.
        at org.postgresql.jdbc.PgStatement.setFetchSize(PgStatement.java:1623)

    原因:JDBC连接参数enableStreamingQuery未设置为true。

  2. 现象:调用Statement、PreparedStatement的execute、executeQuery以及executeUpdate等方法进行数据的查询、插入和更新时出现如下异常:
    org.postgresql.util.PSQLException: Streaming query statement is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming statement sets before attempting more queries.
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:492)
        at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:210)
        at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:147)

    原因:之前进行流式查询的结果集未读取完毕或未关闭。

  3. 只能顺序访问:由于流式查询模式使用的是FORWARD_ONLY的ResultSet,因此只能正向逐行访问数据,无法随机定位或回滚到之前的记录。如果业务逻辑需要多次遍历或随机访问数据,则不能使用流式查询。
  4. 配置与兼容性要求:使用流式查询通常需要在JDBC驱动或ORM框架中做特殊配置(触发条件),如果配置不当,可能无法达到预期效果。