org.hibernate.Session.flush()单独打电话是个好习惯吗?
org.hibernate.Session.flush()
如org.hibernate.Session文档中所述,
org.hibernate.Session
必须在提交事务并关闭会话之前在工作单元的末尾调用(取决于刷新模式,Transaction.commit()会调用此方法)。
您能解释一下flush()是否org.hibernate.Transaction.commit()会明确呼叫的目的吗?
flush()
org.hibernate.Transaction.commit()
在Hibernate手册中,您可以看到此示例
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
您也可以看一下有关冲洗的帖子