Spring Hibernate jdbc批量大小
我有一些场景,我认为从 休眠文档。
数据类:
class HibernateDao {
// ...
@Transactional
public void store(List<Object> batch) {
for(Object o : batch) {
hibernate.store(o);
}
}
}
场景 1
hibernate.jdbc.batch_size = 1
Q1:使用 10
项的集合调用 store(..)
时会发生什么?会有 10 x 1 笔交易还是只有 1 笔?
场景 2
hibernate.jdbc.batch_size = 10
Q2:使用 1
项的集合调用 store(..)
时会发生什么?无论batch_size 属性如何,它都会立即写入后备存储吗?
来自休眠文档:
如果您使用身份标识符生成器,Hibernate 会透明地禁用 JDBC 级别的插入批处理
Q3:什么被分类为 identify 标识符生成器
,使用注释 @Id< /code> 和
@GenerateValue(strategy = GenerationType.AUTO)
?
I have a few scenarios which I think is a little unclear from the hibernate documenation.
Data class:
class HibernateDao {
// ...
@Transactional
public void store(List<Object> batch) {
for(Object o : batch) {
hibernate.store(o);
}
}
}
Scenario 1
hibernate.jdbc.batch_size = 1
Q1: What happens when invoking store(..)
with a collection of 10
items? Will there be 10 x 1 transactions or only one?
Scenario 2
hibernate.jdbc.batch_size = 10
Q2: What happens when invoking store(..)
with a collection of 1
items? Will it immediately be written to the backing store regardless of the batch_size property?
From the hibernate documentation:
Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator
Q3: What is classified as an identify identifier generator
, using the annotations @Id
and @GeneratedValue(strategy = GenerationType.AUTO)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 Spring 事务特有的,但就 Hibernate 而言,如果您使用一个会话和一个“事务”,它将等到“刷新”或“提交”来实际执行操作。所以,一笔交易。
不是立即。与之前的响应相同的内容也适用于此:除非您明确要求“刷新”,否则 Hibernate 将在提交阶段执行该操作。
Hibernate 无法将其预测为 ID 的任何内容。例如,身份(类似于序列,但适用于 T-SQL 数据库)、自动增量、序列……原因很简单:Hibernate 不知道每个批处理实体生成的 ID 是什么,因此,插入后实体的状态将与之前的状态不同。 Hibernate 在常规情况下通过调用“getGenerateKeys”的 JDBC 方法来处理该问题,这允许它将数据库中的数据与其会话中的数据同步,但不可能批量执行此操作。
如果 Hibernate 确实知道实体的 ID(即:分配的、hilo、uuid,...),它可以安全地执行批处理。
This is specific to Spring transactions, but as far as Hibernate is concerned, if you are using one session and one "transaction", it'll wait till "flush" or "commit" to actually perform the operations. So, one transaction.
Not immediately. The same from the previous response applies here: unless you explicitly ask for a "flush", Hibernate will do the action during the commit phase.
Anything which Hibernate cannot predict as the ID. For instance, identity (like sequences, but for T-SQL databases), auto-increment, sequences, ... The reason is simple: Hibernate does not knows what would be the generated ID for each of the batched entities, so, the state of the entity after the insert would be different than the state before. Hibernate handles that in regular scenarios by calling the JDBC method for "getGeneratedKeys", which allows it to synchronize the data from the database with the data in its session, but it's not possible to do it for batches.
If Hibernate does knows what would be the ID for the entities (ie: assigned, hilo, uuid, ...) it can safely perform a batch the execution.