java - JDBC 的executeBatch() 是并行执行语句还是一个接一个地执行语句

标签 java postgresql jdbc

我想从 postgreSQL 数据库中的多个表中删除条目。 这些表有外键约束,所以我只需要按特定顺序删除它们(否则删除将失败)。

我正在考虑将它们添加到批处理中并运行 executeBatch()

我知道executeBatch将所有语句一起提交给驱动程序,但是语句是如何执行的?删除的顺序是否会按照添加到批处理的顺序保持?我找不到 API 文档中提到的

最佳答案

JDBC 4.3 specification在第 14.1.2 节中显式指定批处理执行的行为成功执行:

Batch commands are executed serially (at least logically) in the order in which they were added to the batch.

The entries in the array are ordered according to the order in which the commands were processed (which, again, is the same as the order in which the commands were originally added to the batch).

“至少在逻辑上”为数据库提供了一些重新排序的余地作为优化,只要结果行为与按指定顺序执行批处理相同即可。为了确保返回的更新计数匹配以及异常行为,还需要按顺序执行。

关于java - JDBC 的executeBatch() 是并行执行语句还是一个接一个地执行语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64314317/

相关文章:

java - 在构造函数中抛出异常是否明智?

grails - 如何在Grails中添加jdbc驱动程序以访问Ingres数据库

java - 我应该如何处理我的应用程序中的 url 路由?

java - 我想更改滚动时应用程序工具栏的颜色

java - 为什么我的 Java 程序不更新关联的 postgresql 表?

postgresql - 具有 null 参数的 Postgresql 函数为简单的 case/when/else 生成 null

sql - 递归查询中的排序结果

java - 如何将 jdbc 连接器连接到 MySql 数据库服务器?

java - 准备语句: executeUpdate insert wrongly the raw

java - VS2008+ReSharper 与 IntelliJ IDEA 相比如何?