<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Upcom.eu Blog &#187; jboss</title>
	<atom:link href="http://blog.upcom.eu/tag/jboss/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.upcom.eu</link>
	<description>Comments, remarks, information, solutions from the Upcom team</description>
	<lastBuildDate>Tue, 13 Sep 2011 17:56:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Batch inserts in hibernate</title>
		<link>http://blog.upcom.eu/2008/03/06/batch-inserts-in-hibernate/</link>
		<comments>http://blog.upcom.eu/2008/03/06/batch-inserts-in-hibernate/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 19:21:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[autoCommit]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[inserts]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[relaxAutoCommit]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.source-stream.com/blog/2008/03/06/batch-inserts-in-hibernate/</guid>
		<description><![CDATA[We recently faced a performance issue with the insertion of a few thousand records in the database. Instead of the expected few seconds, the insertions took more than a minute! Checking the logs we realised that the problem was caused &#8230; <a href="http://blog.upcom.eu/2008/03/06/batch-inserts-in-hibernate/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>We recently faced a performance issue with the insertion of a few thousand records in the database. Instead of the expected few seconds, the insertions took more than a minute! Checking the logs we realised that the problem was caused by the number of statements sent to the server than to any other bottleneck &#8211; server load, network bandwidth or other lack of resources.</p>
<p>The solution was to enable batch inserts and we first tried doing it by just setting the attribute <strong>hibernate.jdbc.batch_size</strong> to a relatively high number, 100 in our case. Looking at the logs though that did not have any result, the number of insert statements sent to the database was still the same as before.</p>
<p>Then, we noticed that all our statements with the database had the autocommit set to true; that was disabling by default the creation of batch statements, therefore we had to change it to false; instead of the simple statement<br />
<code>try {<br />
  for (MyObject object : objectList){<br />
     getSession().saveOrUpdate(object);<br />
  }<br />
}<br />
catch (.. ..){<br />
...<br />
}<br />
</code>we had to code the following<code><br />
  try {<br />
  autoCommit = getSession().connection().getAutoCommit();<br />
  getSession().connection().setAutoCommit(false);<br />
  Transaction tx = getSession().beginTransaction();<br />
  for (MyObject object : objectList){<br />
     getSession().saveOrUpdate(object);<br />
  }<br />
  tx.commit();<br />
  }<br />
  catch (.. ..){<br />
  ...<br />
  }</code></p>
<p>Still, the behaviour persisted! The number of insert statements sent to the database server was related to the number of objects to saveorupdate!</p>
<p>Checking again the documentation &#8211; and occasionally the source code of hibernate &#8211; we realised that batch inserts were disabled if the id column of the table was of type <strong><em>Identity</em></strong>. At first we thought this was not a problem as we are using MySql and the generator type is <strong><em>native</em></strong>! The reality is that native maps to Identity for MySql. Overall, I think the lesson learned is that if hinernate needs to get the value of the introduced record from the server, then batch inserts are disabled &#8211; even if you do not need those ids anymore!</p>
<p>Changing the generator to <strong><em>hilo</em></strong> with a corresponding table to hold the generated ids, we seemd to have solved the problem; the number of insert statements was divided by the batch_size parameter, specified above. <em>Be careful: looking at the log4j logs of hibernate the number of Insert statements remains the same, but if you look at the mysql logs, then you see the truth!</em></p>
<p>Then, we faced the final problem: the batch inserts worked perfectly while running the jUnit test, failed completely when the same code was run in the web application.</p>
<p>The problem we faced was <code>java.sql.SQLException: Can't call commit when autocommit=true </code></p>
<p>We thought of a problem with jBoss and the wrapping datasource! Changed it to DBCP and the problem remained! Then, by googling we found quotes mentioning a problem with autocommit and Tomcat. Setting the attribute <strong>relaxAutoCommit=true</strong> in the jdbc url we solved our problem.</p>
<p><code>jdbc:mysql://dbserver/database?rewriteBatchedStatements=true&amp;relaxAutoCommit=true</code></p>
<p>The above setting works well with the DBCP pool, but raises the same exception as above when used with a jboss hibernate configuration file, e.g. mysql-ds.xml.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.upcom.eu/2008/03/06/batch-inserts-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

