-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: HowTo: Getting batch inserts to work on MySQL
PostPosted: Thu Sep 09, 2010 5:01 am 
Newbie

Joined: Thu Sep 09, 2010 4:25 am
Posts: 1
It took me 3 days to get the batch inserts working with a proper performance and I want to share my experience here so others don't have to go through all of this. Finding the right documentation online has been really hard, especially if you don't know what to search for.

Here is what I have done in addition to what is said in chapter 13 of the hibernate docu:

In addition to hibernate.jdbc.batch_size, you should also set hibernate.order_inserts to true if you are inserting more than one Entity class.

The hibernate docu says:
Quote:
Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.


What this actually is trying to say is that if you simply use
Code:
@Id @GeneratedValue
private int id;

it is not going to work. You have to use a generator that supports it. I found that the @TableGenerator works for MySQL. Example:
Code:
@TableGenerator(name = "EntityIdGen", table = "TABLE_GENERATOR", pkColumnValue = "Entity")
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "EntityIdGen")
private int id;


I think the hibernate docu should be updated/clarified because it's really hard to understand those things from the given text.

However, if you enable the MySQL logging you are still not seeing any "Insert into Entity (...) values (...) (...) (...) but only single inserts.
I guess this is because MySQL does not support the standard JDBC batch inserts (??). However, they have a parameter that makes their JDBC driver to transfrom several small inserts into single big one. It's called rewriteBatchedStatements and you can easily set it in your connection url, example:
Code:
<connection-url>jdbc:mysql:///databasename?rewriteBatchedStatements=true</connection-url>


One last thing I noticed, and I couldn't find out what is causing it, is that if you're transaction is getting really big, the commit takes forever. Profiling did not show that it is hibernate causing it, but the com.arjuna.ats ... transaction classes used by JBoss. But it also may be that MySQL is taking forever to do the commit.
To give you an example in times, if i inserted data for 30 seconds, then commiting took like 90 seconds.
I improved my overall to 45 seconds by having a lot of small transactions.
Again, I don't know if it is Hibernate, JBoss or MySQL that is responsible for that, or if this may be gone in future versions.

Hope this saves you some time. Good luck!


Top
 Profile  
 
 Post subject: Re: HowTo: Getting batch inserts to work on MySQL
PostPosted: Sun Sep 12, 2010 4:11 pm 
Newbie

Joined: Sun Sep 12, 2010 4:01 pm
Posts: 1
Man thanks a lot for your post!
It's so incredible, this should be appearing in the official documentation...
Good for you and us...and so bad for Hibernate Documentation.


Top
 Profile  
 
 Post subject: Re: HowTo: Getting batch inserts to work on MySQL
PostPosted: Wed Oct 06, 2010 2:33 pm 
Newbie

Joined: Wed Oct 06, 2010 1:02 pm
Posts: 1
Hi,
Is it possible to achieve the same thing – a single big insert instead of a lot of small ones – with PostgreSQL?
I guess the rewrite is a MySQL specific feature? I tried to follow the above-describe procedure but I didn’t get it to work with PostgreSQL. According to the connection metadata the driver should support batch updates. Is Hibernate its self not able to create the multi-entry inserts?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.