为什么 Hibernate 在使用 IDENTITY 标识符生成器时禁用 INSERT 批处理 [英] Why does Hibernate disable INSERT batching when using an IDENTITY identifier generator

查看:22
本文介绍了为什么 Hibernate 在使用 IDENTITY 标识符生成器时禁用 INSERT 批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate 文档说:

The Hibernate documentation says:

Hibernate 在 JDBC 级别透明地禁用插入批处理,如果您使用身份标识符生成器.

Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.

但我所有的实体都有这样的配置:

But all my entities have this configuration:

@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
private Integer id;

当我在上面使用这个身份时

When I'm using this identity above So

  1. IDENTITY 有什么问题?
  2. 是否禁用了批量插入?
  3. 我该如何解决这个问题?

推荐答案

Transactional write-behind

Hibernate 尝试将 Persistence Context 刷新推迟到最后一刻.这种策略传统上被称为事务性后写.

Transactional write-behind

Hibernate tries to defer the Persistence Context flushing up until the last possible moment. This strategy has been traditionally known as transactional write-behind.

write-behind 与 Hibernate 刷新更相关,而不是任何逻辑或物理事务.在一个事务中,flush可能会发生多次.

The write-behind is more related to Hibernate flushing rather than any logical or physical transaction. During a transaction, the flush may occur multiple times.

刷新的更改仅对当前数据库事务可见.在提交当前事务之前,其他并发事务不会看到任何更改.

The flushed changes are visible only for the current database transaction. Until the current transaction is committed, no change is visible by other concurrent transactions.

IDENTITY 生成器允许 intbigint 列根据需要自动递增.增量过程发生在当前正在运行的事务之外,因此回滚可能最终会丢弃已分配的值(可能会发生值差距).

The IDENTITY generator allows an int or bigint column to be auto-incremented on demand. The increment process happens outside of the current running transaction, so a roll-back may end up discarding already assigned values (value gaps may happen).

增量过程非常有效,因为它使用数据库内部轻量级锁定机制,而不是更重量级的事务性粗粒锁.

The increment process is very efficient since it uses a database internal lightweight locking mechanism as opposed to the more heavyweight transactional course-grain locks.

唯一的缺点是我们无法在执行 INSERT 语句之前知道新分配的值.这种限制阻碍了 Hibernate 采用的事务性 write-behind 刷新策略.因此,Hibernates 使用 IDENTITY 生成器禁用了对实体的 JDBC 批处理支持.

The only drawback is that we can’t know the newly assigned value prior to executing the INSERT statement. This restriction is hindering the transactional write-behind flushing strategy adopted by Hibernate. For this reason, Hibernates disables the JDBC batch support for entities using the IDENTITY generator.

唯一的解决方案是使用由 pooled-lo 优化器.这个生成器也适用于 MySQL,因此它克服了缺乏数据库 SEQUENCE 支持的问题.

The only solution would be to use a TABLE identifier generator, backed by a pooled-lo optimizer. This generator works with MySQL too, so it overcomes the lack of database SEQUENCE support.

然而,TABLE 生成器的性能比 IDENTITY 差,所以最终,这不是一个可行的替代方案.

However, the TABLE generator performs worse than IDENTITY, so in the end, this is not a viable alternative.

因此,在 MySQL 上使用 IDENTITY 仍然是最好的选择,如果您需要批量插入,您可以使用 JDBC.

Therefore, using IDENTITY is still the best choice on MySQL, and if you need batching for insert, you can use JDBC for that.

这篇关于为什么 Hibernate 在使用 IDENTITY 标识符生成器时禁用 INSERT 批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆