简体   繁体   English

通过显式调用session.flush()设置Hibernate属性hibernate.jdbc.batch_size

[英]Setting Hibernate property hibernate.jdbc.batch_size over explicitly calling session.flush()

Going through the batch processing document of hibernate https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html . 浏览hibernate的批处理文档https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html I tried an example setting the batch size property( hibernate.jdbc.batch_size=20 ) in hibernate configuration file and then doing flush and clear like: 我尝试了一个在hibernate配置文件中设置批处理大小属性( hibernate.jdbc.batch_size = 20 )的示例,然后进行刷新和清除,如下所示:

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

Now the thing is if I set the value of i to 50, then it flushes the records as a batch of 50. So, if I can control the batch size here in the loop , why do I need to set hibernate.jdbc.batch_size 20 property. 现在的事情是,如果我将i的值设置为50,那么它将刷新记录作为批处理50。因此,如果我可以在循环中控制批处理大小,为什么还要设置hibernate.jdbc.batch_size 20个物业。

Can someone please explain the difference between the two approaches ie setting the property over calling flush explicitly. 有人可以解释两种方法之间的区别吗,即通过显式调用flush来设置属性。

My understanding is that your loop and hibernate.jdbc.batch_size operate on different levels. 我的理解是,您的循环和hibernate.jdbc.batch_size在不同的级别上运行。

Your loop operates on entities (of your choice). 您的循环在(您选择的)实体上运行。 But a single Customer might need multiple inserts (for example, when a Customer has multiple Adresses). 但是单个客户可能需要多个插入内容(例如,当一个客户具有多个地址时)。 All these inserts get executed as simple single inserts with your loop approach. 使用循环方法,所有这些插入都将作为简单的单个插入执行。 The flush and clear just prevent the session to grow without limit. flushclear只会阻止会话无限制地增长。

hibernate.jdbc.batch_size on the other hand will bundle (insert)statements that are identical and only differ in in the parameter values into a single statement, with a list of parameter sets. 另一方面, hibernate.jdbc.batch_size会将相同且仅在参数值上不同的语句捆绑(插入)到一个带有参数集列表的语句中。 The execution of such a batched statement should be processed much more efficient by the database then the equivalent single statements. 这样的批处理语句的执行应该比等效的单个语句更有效地由数据库处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM