nanpakal
nanpakal

Reputation: 1021

JPA customize the JDBC batch size is not working

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>

But it trigger 10 insert queries.

i get this queries Hibernate: select charge.nextval from dual 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 (?, ?) Hibernate: insert into CHARGE (ACCOUNTNUMBER CHARGE_ID) values (?, ?)

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

Upvotes: 1

Views: 2334

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153710

The Hibernate logging mechanism might have misled you here. It's hard to see JDBC batching working using the default Hibernate logging.

If you switch to datasource-proxy, then you can see batching working:

Name:DATA_SOURCE_PROXY, Time:6, Success:True,
Type:Prepared, Batch:True, QuerySize:1, BatchSize:3,
Query:["insert into post (title, version, id) values (?, ?, ?)"],
Params:[(Post no. 0, 0, 0), (Post no. 1, 0, 1), (Post no. 2, 0, 2)]

The Batch:True parameter shows you that JDBC catching is working and the BatchSize:3 shows that you have sets of parameter values that are sent in one database roundtrip.

Upvotes: 2

Related Questions