簡體   English   中英

Hibernate JDBC批處理大小不起作用

[英]Hibernate JDBC Batch size is not working

我正在使用SpringFramework 3和Hibernate 4以及MySQL 5和jpa。 我的測試代碼看起來像......

@Repository
public class TestRepositoryImpl implements TestRepository {

  @PersistenceContext
  private EntityManager em;

  @Override
  @Transactional
  public void insertBulk() {
     Item it;
     for(int i= 0; i<1000;i++) {
        it = new Item();
        it.setPrice(Math.random()*100);
        em.persist(it);
     }
  }
}

我的彈簧配置

 <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="application" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

我的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">

<persistence-unit name="application" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.springapp.test.domain.Item</class>
    <class>com.springapp.test.domain.Order</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="false" />
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testdb" />
        <property name="hibernate.connection.username" value="root" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.jdbc.batch_size" value="20" />
    </properties>
</persistence-unit>

</persistence>

當我調用運行我的代碼時,它將觸發插入查詢1000次而不是觸發50插入查詢。 有什么問題? 請幫助我使用hibernate在jpa中批量插入

請注意,如果插入表的主鍵是GenerationType.Identity,Hibernate將透明地禁用JDBC級別的插入批處理。

save()只有一個記錄,然后是flush(),因此每次刷新只需要處理一個INSERT SQL。 這就是Hibernate無法幫助您批量插入的原因,因為只有一個INSERT SQL需要處理。 在調用flush()之前,應該將()保存到最多一定數量的記錄,而不是為每個save()調用flush()。

批量插入的最佳做法是這樣的:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for  ( int i=0; i<888888; i++ ) {
  TableA record = new TableA();
    record.setXXXX();
    session.save(record)
    if ( i % 50 == 0 ) { //50, same as the JDBC batch size
        //flush a batch of inserts and release memory:
        session.flush();
        session.clear();
    }
}
tx.commit();
session.close();

您可以批量保存和刷新記錄。 在每個批處理結束時,您應該清除持久性上下文以釋放一些內存以防止內存耗盡,因為每個持久對象都放在第一級緩存(您的JVM內存)中。 您還可以禁用二級緩存以減少不必要的開銷。

請查看此鏈接http://docs.jboss.org/hibernate/orm/3.5/reference/en/html/batch.html

你誤解了“批量大小”。 批量大小意味着一次性發送“批量大小”數量的查詢,而不是在代碼觸發查詢時發送每個查詢。 因此,在這種情況下,將有1000個插入查詢,在每個批次中發送50次,每次有20個插入查詢。

在級別調試中添加logger org.hibernate.engine.jdbc.batch.internal.BatchingBatch 如果插入順序錯誤,hibernate可以生成大小為1或2的批處理。 嘗試使用hibernate.order_inserts=true hibernate.order_updates=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM