本文介绍了JPA自定义JDBC批处理大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试配置@Vlad Mihalcea博客中提到的JDBC批处理大小 https://vladmihalcea.com/如何为每个持久化上下文自定义jdbc批处理大小与休眠/

I tried to configure JDBC batch size as mentioned in the @Vlad Mihalcea blog https://vladmihalcea.com/how-to-customize-the-jdbc-batch-size-for-each-persistence-context-with-hibernate/

EntityManager entityManager =  entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.unwrap(Session.class).setJdbcBatchSize(5);
        for(int i = 0;i<10;i++){
            Charge c = new Charge();
            c.setAccountNumber("acct"+i);
            entityManager.persist(c);
        }
entityManager.getTransaction().commit();



<bean id="entityManagerFactoryDefault" 
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="charg" />
        <property name="persistenceUnitName" value="MaterializedView" />
        <property name="persistenceXmlLocation" value="classpath*:META-INF/jpa-persistence.xml" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="false" />
                <property name="showSql" value="true"/> 
                <property name="database">
                    <util:constant static-field="org.springframework.orm.jpa.vendor.Database.ORACLE" />
                </property>
            </bean>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.default_batch_fetch_size">500</prop>
                <prop key="hibernate.jdbc.fetch_size">10</prop>
                <prop key="hibernate.jdbc.batch_versioned_data">true</prop>
                <prop key="hibernate.order_updates">true</prop>
                <prop key="hibernate.order_inserts">true</prop>
                <prop key="hibernate.jdbc.batch_size">30</prop>
                <prop key="hibernate.id.new_generator_mappings">false</prop>
            </props>
        </property>

    </bean>

但是它会触发10个插入查询.

But it trigger 10 insert queries.

我得到这个查询休眠:从双选择charge.nextval休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)休眠:插入CHARGE(ACCOUNTNUMBER CHARGE_ID)值(?,?)

i get this queriesHibernate: select charge.nextval from dualHibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)

我正在使用休眠5.2.10.最终版本和序列作为策略.如果我的代码有问题,有人可以纠正我吗

I am using hibernate 5.2.10.Final version and sequence as strategy.Can someone correct me if something wrong in my code

推荐答案

Hibernate日志记录机制可能在这里误导了您.

The Hibernate logging mechanism might have misled you here.

使用数据源代理来查看批处理是否有效.查看这篇文章以了解更多详细信息.

Use datasource-proxy to see if batching works. Check out this article for more details.

这篇关于JPA自定义JDBC批处理大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 23:59