Jooq 3.14.1 fetchLazy 与 postgres 获取所有行

Jooq 3.14.1 fetchLazy with postgres fetching all rows

我是 jooq 的新手,正在尝试将 fetchLazy 与 postgres 一起使用。

我修改了article中提到的数据访问代码,如下所示

// BookService.java
public List<Book> getBooks(){
    final List<Book> books = new ArrayList<>();
    ResultQuery<BookRecord> resQuery = context.selectFrom(Tables.BOOK).fetchSize(2);
    transactionRunner.readOnlyTransaction(() -> {
        try(final Cursor<BookRecord> bookRecords = resQuery.fetchLazy()) {
            while (bookRecords.hasNext()) {
                List<Book> into = bookRecords.fetch().into(Book.class);
                System.out.println("Num Records: " + into.size());
                books.addAll(into);
            }
        }
    });
    return books;
}

我的交易代码如下所示 -

public class TransactionalRunner {
  private final PlatformTransactionManager manager;

  public void readOnlyTransaction(Runnable fn) {
    var template = new TransactionTemplate(manager);
    template.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
    template.execute(
        transactionStatus -> {
          fn.run();
          return null;
        });
  }
}

为了测试,我在数据库中添加了 4 条记录。即使 fetchSize 设置为 2.

,我还是在单次提取中获取了所有 4 条记录
Num Records: 4

所有修改代码放在github.

谁能告诉我出了什么问题?

Postgres 是 运行 作为 docker

docker run --name tuk-temp -p 58704:5432 -e POSTGRES_PASSWORD=postgres postgres

大小需要在fetchNext中指定。

// BookService.java
public List<Book> getBooks(){
    final List<Book> books = new ArrayList<>();
    ResultQuery<BookRecord> resQuery = context.selectFrom(Tables.BOOK).fetchSize(2);
    transactionRunner.readOnlyTransaction(() -> {
        try(final Cursor<BookRecord> bookRecords = resQuery.fetchLazy()) {
            while (bookRecords.hasNext()) {
                List<Book> into = bookRecords.fetchNext(2).into(Book.class);
                System.out.println("Num Records: " + into.size());
                books.addAll(into);
            }
        }
    });
    return books;
}