JDBC的executeBatch()方法会并行执行语句还是依次执行?

huangapple go评论75阅读模式
英文:

Does JDBC's executeBatch() execute statements in parallel or one after the other

问题

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

我考虑将它们添加到一个批处理中,然后运行 executeBatch()

我理解 executeBatch 会将所有语句一起提交给驱动程序,但语句是如何执行的呢?删除的顺序是否将按照添加到批处理的顺序进行维护?我在 API 文档中找不到相关提及。

英文:

I want to delete entries from multiple tables in a postgreSQL DB.
The tables have foreign key constraints, so I need to delete them in particular order only (otherwise delete will fail).

I am thinking of adding them to a batch and running executeBatch()

I understand that executeBatch submits all the statements together to driver but how are statements executed? Is the order of deletion will be maintained as per order of adding to the batch? I can't find it mentioned in API doc

答案1

得分: 3

JDBC 4.3 specification明确规定了批量执行的行为,具体在第14.1.2节“成功执行”中:

> 批命令按照它们被添加到批中的顺序(至少在逻辑上)依次执行。

以及

> 数组中的条目的顺序是根据命令被处理的顺序排序的(同样,这与命令最初添加到批中的顺序相同)。

“至少在逻辑上”为数据库提供了某种余地,以便进行重新排序以进行优化,只要最终的行为与按照指定顺序执行批处理时的行为相同。按顺序执行还是必要的,以确保返回的更新计数匹配,并且用于异常行为。

英文:

The JDBC 4.3 specification explicitly specifies the behaviour of a batch execution in section 14.1.2 Successful Execution:

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

and

> 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).

The "at least logically" gives databases some leeway to reorder things as an optimization, as long as the resulting behaviour is the same as if the batch was executed in the specified order. Execution in-order is also necessary to ensure the returned update counts match, and for exception behaviour.

答案2

得分: 1

它们按顺序执行。

"批处理"的目的是收集SQL语句,并将它们作为一个块、一系列语句传输,以减少与数据库服务器通信的网络开销。

完整的"发送SQL,等待响应"需要时间,因此通过一起发送多个请求,可以消除大量的等待时间。

英文:

They are executed in order.

The purpose of "batching" is to collect the SQL statements and transmit them as a block, a sequence of statements, in order to reduce the network overhead of communicating with the database server.

A full "send SQL, wait for response" takes time, so by sending multiple requests together, a lot of waiting time can be eliminated.

huangapple
  • 本文由 发表于 2020年10月12日 16:34:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定