我想从 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/