java - 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
?
解决方案
从 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 做的两件事你可能不需要自己做:
- 它包装
CompletableFuture
在一个代理中,该代理保留对您的引用Executor
,以便也在该执行程序上运行后续任务,而不是默认返回到 commonForkJoinPool
。IMO 的设计存在重大错误CompletableFuture
。 - 它将同步执行 (
this::execute
) 包装在一个blocking()
包装器中,该包装器方便地将逻辑包装在ForkJoinPool.managedBlock()
. 在运行阻塞工作时建议这样做ForkJoinPool
推荐阅读
- r - 如何在 R 格式表中隐藏多个列名?
- python - Tensorflow 1.3 中的 InvalidArguementError
- angular - 角度 11 中的热模块更换
- squid - 当身份验证包含在 squid 配置文件中时,Squid 代理拒绝规则不起作用
- python - 获取正在运行的文件的路径,而不是调用来自的目录
- html - 将标签链接到 html / css 中的重点输入限制的问题
- postgresql - 无法在 Ubuntu 上安装 pgaudit
- mysql - MySQL游标不适用于日期变量
- python - Openpyxl 无法加载工作簿
- r - 在 R 中用 0 拟合 beta 分布