4

We are using container managed transaction . Application server JBOSS AS7 . I have enabled the below properties in hibernate configuration file

<property name="hibernate.connection.url">jdbc:mysql://localhost/test?rewriteBatchedStatements=true</property> 
<property name="hibernate.jdbc.batch_size" value="20"/> 
<property name="hibernate.order_inserts" value="true"/> 
<property name="hibernate.order_updates" value="true"/> 
<property name="hibernate.jdbc.batch_versioned_data" value="true"/>

And enabled the logging property as below

<logger category="org.hibernate.SQL">
    <level name="TRACE"/>
</logger>

I am inserting 10 records to the database. In the hibernate.log , i could see 10 insert statements as below

org.hibernate.sql insert into test (name,value,date) values (?,?,?) org.hibernate.sql insert into test (name,value,date) values (?,?,?)  

The batch insert didn't work

Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
Saranya
  • 71
  • 8

1 Answers1

3

Very probably you are actually using batching; it is just that Hibernate prints separate sql for each entity instance.

To check this, enable DEBUG log level for org.hibernate package (and TRACE level for org.hibernate.type if you want to see bound variables), then check if the following phrases appear in the log:

  • Reusing batch statement

  • Executing batch size

If a number larger than 1 is printed for executed batch size, then you are using batching.

Specific to MySQL, to make sure that MySQL driver rewrites insert statements, enable profileSQL parameter in connection url as described here.

NOTE: JDBC batching is disabled if IDENTITY id generator is used.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
  • Yes. I have enabled the logs as said above. Executing batch size logs as "1". Also the "Reusing batch statement" does not appear. – Saranya Jun 23 '15 at 06:25
  • Also to note, we are using stateless session bean. So is this the reason for not using batch? – Saranya Jun 23 '15 at 07:04
  • I have not tried with stateful session bean. Found in some blogs that stateless session bean doesn't support first level and second level cache of hibernate, so each query will be sent to the database. – Saranya Jun 23 '15 at 11:22
  • It should not be related to that. Is the identifier generation strategy IDENTITY? – Dragan Bozanovic Jun 23 '15 at 11:23
  • No. Identifier generator is TABLE. – Saranya Jun 23 '15 at 11:25
  • So, everything happens in one transaction? – Dragan Bozanovic Jun 23 '15 at 11:31
  • Yes it happens in single transaction – Saranya Jun 23 '15 at 11:35
  • Then I'm out of options, maybe you could post the relevant code parts. Maybe some automatic flush occurs between the inserts, or something like that. – Dragan Bozanovic Jun 23 '15 at 11:37
  • @TransactionAttribute(TransactionAtrributeType.REQUIRES_NEW) – Saranya Jun 23 '15 at 12:03
  • @TransactionAttribute(TransactionAtrributeType.REQUIRES_NEW) public void (List test){ for(Test i:test){ EntityManager.persist(i); } } Whether i am missing anything else. – Saranya Jun 23 '15 at 12:09
  • @Saranya Please see my edited answer. Did you try using `profileSQL` parameter to check if MySQL driver rewrites statements? – Dragan Bozanovic Jun 24 '15 at 17:03
  • @Dragon The flush mode was set to auto.After i changed the flush mode,batch update was working.Now i could see below lines in the log. 1.Executing batch size: 10 2. Reusing prepared statement. Whether this was correct. – Saranya Jun 25 '15 at 07:43
  • This should not depend on the flush mode. To which value did you set it? – Dragan Bozanovic Jun 25 '15 at 08:53
  • With that you have basically turned off automatic flush before queries. While there isn't anything wrong with that (actually this can be a nice performance improvement), don't forget to manually flush the session before a query if the query depends on the session's dirty (unflashed) state. – Dragan Bozanovic Jun 25 '15 at 09:44
  • However, it's still a little bit odd that it affects batch inserts. You are sure that there are no queries executed in the loop in which you persist `Test` entities, thus causing flush to occur in each iteration? – Dragan Bozanovic Jun 25 '15 at 09:48