首页 > 解决方案 > jOOQ asynchronous batch insert

问题描述

I want to insert a large number of rows into my database. I've seen that jOOQ has batch support, so I can do the following:

dslContext.batchInsert(myListOfRecords).execute()

However, this is synonymous. jOOQ has many other asynchronous APIs, is there one for batch inserts?

If not, is it safe to simply wrap the call to execute in a CompletableFuture?

标签: javaasynchronousjooq

解决方案


从 jOOQ 3.12 开始,类型executeAsync()上确实缺少这些方法org.jooq.Batch。见:https ://github.com/jOOQ/jOOQ/issues/9806

然而,他们并没有做任何魔法。如果您查看 的实现AbstractQuery.executeAsync(Executor),这就是它正在做的事情(从 jOOQ 3.12 开始):

@Override
public final CompletionStage<Integer> executeAsync(Executor executor) {
    return ExecutorProviderCompletionStage.of(
        CompletableFuture.supplyAsync(blocking(this::execute), executor), () -> executor
    );
}

jOOQ 做的两件事你可能不需要自己做:

  1. 它包装CompletableFuture在一个代理中,该代理保留对您的引用Executor,以便也在该执行程序上运行后续任务,而不是默认返回到 common ForkJoinPool。IMO 的设计存在重大错误CompletableFuture
  2. 它将同步执行 ( this::execute) 包装在一个blocking()包装器中,该包装器方便地将逻辑包装在ForkJoinPool.managedBlock(). 在运行阻塞工作时建议这样做ForkJoinPool

推荐阅读