session.clear()在Hibernate中如何工作 [英] How does session.clear() work in Hibernate

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

问题描述

我提到了很多文章,但仍然不清楚session.clear在休眠状态下的表现.

I referred many articles but still I still not clear on what session.clear perform in hibernate.

根据我到目前为止遇到的是, 当我们使用批量保存/更新时,如下所示:

According to what I came across so far is, When we use batch save / update as shown below:

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Employee employee = new Employee(.....);
    session.save(employee);
    if( i % 50 == 0 ) { // Same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}
tx.commit();
session.close();

sesion.flush();用于刷新会话强制Hibernate将Session的内存中状态与数据库同步.

sesion.flush(); is used in flushing the session forces Hibernate to synchronize the in-memory state of the Session with the database.

问题

1..刷新会话后,为什么需要执行session.clear()?真的需要吗?

1. After flushing the session, why is it necessary to do session.clear()? Is it really required ?

2..session.clear()会执行提交操作吗?

2. Will session.clear() perform commit action ?

3..如果session.clear()逐出所有已加载的对象,那么在执行提交和回滚操作时内部会发生什么?

3. If session.clear() evicts all loaded objects, what happens internally while commit and rollback actions are performed ?

推荐答案

将Session看作是您自开始当前事务以来已从数据库加载(或持久存储到)的实体的缓存.

Think of the Session as a cache of entities you have already loaded from (or persisted to) the database since you've started the current transaction.

  1. Session.clear在任何情况下都不是强制性的,但是如果您在一个事务中执行大量实体加载/保存操作,则很有用,以避免内存不足错误.在您的示例中,您将在会话中复制50个employee实体.没有flushclear方法每隔50个save()调用,您将在会话中复制100.000个实体(并且由于会话具有指向该实体的链接,因此无法进行垃圾回收).

  1. Session.clear is not mandatory in any way, but is useful if you do a lot of entity loading/saving within one transaction, to avoid getting out of memory error. In your example, you'll have 50 employee entities replicated in the session. Without the flush and clear method call every 50 save() you would have had 100.000 entities replicated in the session (and not garbage collectable, because the session has a link to the entity).

Session.clear将不执行提交或回滚.甚至不是flush(因此,为什么应该在Session.clear之前进行刷新,以便hibernate生成SQL查询来查询待处理的实体.

Session.clear will not perform commit nor rollback. Not even a flush (hence why you should do flush before a Session.clear, so that hibernate generates sql queries for pending entities updates.

回滚或提交操作不在应用程序端执行,而是在数据库中执行:hibernate只会要求数据库进行提交或回滚(Hibernate可能会在提交操作之前触发刷新,但是刷新不是一部分)的提交).提交操作将(也无法)访问会话.这是一种数据库内部机制,由于自事务开始以来运行了所有SQL查询,因此该数据库内部机制将保留(或还原)执行的数据修改.

Rollback or commit actions are not performed on the application side, but in the database : hibernate will just ask the database to commit or rollback (Hibernate might trigger a flush before the commit action, but the flush is not part of the commit). The commit action will not (and can't) access the session. It is an database internal mechanism that will persist (or revert) the data modification performed thanks to all SQL queries run since the start of the transaction.

以相同的方式,在休眠状态下打开事务并没有执行很多操作:主要是从池中获取数据库连接,并在sql之后告诉数据库 NOT auto_commit查询,但要等待提交或回滚命令.

Exactly in the same way, opening a transaction in hibernate is not performing a lot of things : mainly getting a db connection out of the pool, and telling the database to NOT auto_commit following sql queries but to wait for a commit or rollback command.

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

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