关于Hibernate的问题session.flush() [英] Question about Hibernate session.flush()

查看:186
本文介绍了关于Hibernate的问题session.flush()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for(int i = 0; 

)我想询问实际上flush方法在下面的情况下做了些什么: i< myList.size(); i ++){
Car c = new Car(car.get(i).getId(),car.get(i).getName());
getCurrentSession()。save(c);
if(i%20 == 0)
getCurrentSession()。flush();





$ b

这是否意味着迭代20后,缓存被刷新,然后20个持有的内存对象实际上保存在数据库中?



有人请向我解释当条件为真时会发生什么。

解决方案

来自 Session#flush


强制此会话刷新。必须在工作单元结束时调用
,提交事务前
,关闭会话
(取决于
flush-mode Transaction.commit()
调用此方法)。
$ b

Flushing 是将底层
持久性存储和内存中持久性
状态同步的过程。 / b>



换句话说, flush 告诉Hibernate执行同步所需的SQL语句JDBC连接的状态与会话级缓存中保存的对象状态。并且条件 if(i%20 == 0)将使每个 i 的倍数为20。 / p>

但是,新的 Car 实例仍然保存在会话级缓存中,对于大 myList.size(),你将吃掉所有的内存并最终得到 OutOfMemoryException 。为避免出现这种情况,文档中描述的模式为 flush AND clear 会定期(与JDBC批量大小相同)保持更改,然后分离实例以便它们可以被垃圾收集:


< h3> 13.1。批量插入

定期创建新对象
flush()然后清除()会话
以控制大小为
的一级缓存。

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

(int i = 0; i <100000; i ++){
Customer customer = new Customer(.....);
session.save(customer);
if(i%20 == 0){// 20,与JDBC批量大小相同
//刷新一批插入并释放内存:
session.flush();
session.clear();
}
}

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


文档在同一章节中提到了如何设置JDBC批处理大小。

另见




I want to inquire about what actually the flush method does in the following case:

for (int i = 0; i < myList.size(); i++) {
    Car c = new Car( car.get(i).getId(),car.get(i).getName() );
    getCurrentSession().save(c);
    if (i % 20 == 0)
        getCurrentSession().flush();
}

Does this means that after the iteration 20, the cache is flushed, and then the 20 held memory objects are actually saved in the database ?

Can someone please explain to me what will happen when the condition is true.

解决方案

From the javadoc of Session#flush:

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

Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.

In other words, flush tells Hibernate to execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in the session-level cache. And the condition if (i % 20 == 0) will make it happen for every i multiple of 20.

But, still, the new Car instances will be held in the session-level cache and, for big myList.size(), you're going to eat all memory and ultimately get an OutOfMemoryException. To avoid this situation, the pattern described in the documentation is to flush AND clear the session at regular intervals (same size as the JDBC batch size) to persist the changes and then detach the instances so that they can be garbage collected:

13.1. Batch inserts

When making new objects persistent flush() and then clear() the session regularly in order to control the size of the first-level cache.

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();

The documentation mentions in the same chapter how to set the JDBC batch size.

See also

这篇关于关于Hibernate的问题session.flush()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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