gpt4 book ai didi

java - hibernate :flush() 和 commit()

转载 作者:IT老高 更新时间:2023-10-28 13:52:40 26 4
gpt4 key购买 nike

单独调用 org.hibernate.Session.flush() 是个好习惯吗?

正如 org.hibernate.Session 文档中所说,

Must be called at the end of a unit of work, before commiting the transaction and closing the session (depending on flush-mode, Transaction.commit() calls this method).

如果 org.hibernate.Transaction.commit() 已经可以调用 flush(),你能解释一下调用的目的吗?

最佳答案

在 Hibernate Manual 你可以看到这个例子

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

for (int i = 0; i < 100000; i++) {
Customer customer = new Customer(...);
session.save(customer);
if (i % 20 == 0) { // 20, same as the JDBC batch size
// flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}

tx.commit();
session.close();

如果不调用 flush 方法,你的一级缓存会抛出 OutOfMemoryException

Also you can look at this post about flushing

关于java - hibernate :flush() 和 commit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14581865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com