Hibernate JDBC批处理大小无效。

8

我正在使用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);
     }
  }
}

我的Spring配置

 <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中进行批量插入。

请添加堆栈跟踪。 - Sureshkumar Panneerselvan
抱歉...这段代码没有任何异常或错误。代码运行良好,但在控制台上会打印1000次插入查询。 - Mitesh
3个回答

9
请注意,如果插入表的主键是GenerationType.Identity,则Hibernate会在JDBC级别自动禁用插入批处理。
save()方法只能保存一条记录并刷新缓存,因此每次刷新只会有一条INSERT SQL语句被处理。这就是为什么Hibernate不能帮助您进行批量插入,因为只有一条INSERT SQL需要被处理。您应该在调用flush()之前将多条记录保存到一定数量,而不是每次保存都调用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

你的代码片段运行良好。但我认为这段代码是特定于Hibernate的代码,我想使用不特定于Hibernate的JPA。我的JPA可能是Hibernate、EclipseLink或OpenJPA中的一个提供程序。 - Mitesh
1
请查看以下链接: https://dev59.com/qnE85IYBdhLWcg3wdDEM https://access.redhat.com/site/documentation/en-US/JBoss_Enterprise_Application_Platform/5/html/Performance_Tuning_Guide/sect-Performance_Tuning_Guide-Entity_Beans-Batch_Inserts.html - Sureshkumar Panneerselvan
请查看此答案,了解为什么Hibernate在使用标识符ID生成器时禁用批量更新以及您可以采取的措施:https://dev59.com/MF4c5IYBdhLWcg3wn7qj - Innokenty

2

在调试级别中添加记录器org.hibernate.engine.jdbc.batch.internal.BatchingBatch。 如果插入顺序错误,Hibernate可以生成大小为1或2的批处理。 尝试使用hibernate.order_inserts=true hibernate.order_updates=true


1
你误解了“批量大小”的含义。批量大小是指将“批量大小”数量的查询一起发送,而不是在代码触发查询时逐个发送每个查询。因此,在这种情况下,将发送1000个插入查询,分50次发送,每个批次包含20个插入查询。

我想做类似于这个链接的事情。 - Mitesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接