Jooq 3.14.1 fetchLazy 使用 postgres 获取所有行

发布于 2025-01-14 00:46:43 字数 1863 浏览 1 评论 0 原文

我是 jooq 新手,并尝试使用 fetchLazy< /a> 与 postgres。

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

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

有人可以让我知道出了什么问题吗?

  • Jooq 版本 3.14.1

Postgres 作为 docker 运行

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

I am new to jooq and trying to use fetchLazy with 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. I am getting all 4 records in the single fetch even though fetchSize is set to 2.

Num Records: 4

All the modified code is placed in github.

Can someone let me know what is going wrong?

  • Jooq Version 3.14.1

Postgres is running as docker

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2025-01-21 00:46:43

需要在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;
}

Size needs to be specified in 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文