Hibernate批处理的用途是什么? [英] What is the use of Hibernate batch processing

查看:103
本文介绍了Hibernate批处理的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  

我对hibernate有所了解我对hibernate批处理有疑问, Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for(int i = 0; i <100000; i ++)
{
Employee employee = new Employee(.....);
session.save(employee);

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

Hibernate会缓存会话级缓存中的所有持久对象,最终您的应用程序将会OutOfMemoryException在第50,000行左右。你可以解决这个问题,如果你正在使用Hibernate进行批处理,比如

  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)
{//与JDBC批量大小相同
//刷新一批插入并释放内存:
session.flush() ;
session.clear();
}
}
tx.commit();
session.close();

我怀疑是不是初始化外部会话,为什么我们不能初始化它到for循环,

  Session session = null; 
Transaction tx = session.beginTransaction();
for(int i = 0; i <100000; i ++)
{
session = SessionFactory.openSession()
Employee employee = new Employee(.....);
session.save(employee);
}
tx.commit();
session.close();

这是正确的方式还是没有人建议我使用正确的方式?

解决方案

没有。不要在for循环中初始化会话;每次你开始一个新的会话时,你都会开始一个新的批处理(所以你的批量大小是你的方式,也就是说它不是批处理)。另外,它会比你的方式慢得多。这就是为什么第一个例子有

$ p $ if(i%50 == 0){
// flush a batch插入和释放内存:
session.flush();
session.clear();
}

这就是冲洗一批插入和释放内存的原因。

I am new to hibernate i have doubt in hibernate batch processing, i read some tutorial for hibernate batch processing they said

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) 
{
  Employee employee = new Employee(.....);
  session.save(employee);

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

Hibernate will cache all the persisted objects in the session-level cache and ultimately your application would fall over with an OutOfMemoryException somewhere around the 50,000th row. You can resolve this problem if you are using batch processing with Hibernate like,

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

My doubt is instead of initializing the session outside, why can't we initialize it in to the for loop like,

Session session = null;
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) 
{
 session =SessionFactory.openSession()
 Employee employee = new Employee(.....);
 session.save(employee);
}
tx.commit();
session.close();

Is it correct way or not any one suggest me the correct way?

解决方案

No. Don't initialize the session in the for loop; every time you start a new session you're starting a new batch (so you have a batch size of one your way, that is it is non-batching). Also, it would be much slower your way. That is why the first example has

if( i % 50 == 0 ) { 
  //flush a batch of inserts and release memory:
  session.flush();
  session.clear();
}

that is what "flush a batch of inserts and release memory" was for.

这篇关于Hibernate批处理的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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