简体   繁体   English

Jooq 3.14.1 fetchLazy 与 postgres 获取所有行

[英]Jooq 3.14.1 fetchLazy with postgres fetching all rows

I am new to jooq and trying to use fetchLazy with postgres.我是 jooq 的新手,正在尝试将fetchLazy与 postgres 一起使用。

I modified the data access code as mentioned in this article like below我修改了本文中提到的数据访问代码,如下所示

// 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;
}

My transaction code looks like below -我的交易代码如下所示 -

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;
        });
  }
}

For testing, I have added 4 records in database.为了测试,我在数据库中添加了 4 条记录。 I am getting all 4 records in the single fetch even though fetchSize is set to 2.即使fetchSize设置为 2,我在单次提取中获取了所有 4 条记录。

Num Records: 4

All the modified code is placed ingithub .所有修改后的代码都放在github中。

Can someone let me know what is going wrong?有人可以让我知道出了什么问题吗?

  • Jooq Version 3.14.1 Jooq版本3.14.1

Postgres is running as docker Postgres 作为 docker 运行

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

Size needs to be specified in fetchNext .需要在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;
}

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

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