<?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>Robin&#039;s Tech Tips &#187; openjpa</title>
	<atom:link href="http://robin.mytechtip.com/tag/openjpa/feed/" rel="self" type="application/rss+xml" />
	<link>http://robin.mytechtip.com</link>
	<description>Just another My Tech Tip weblog</description>
	<lastBuildDate>Wed, 06 Jul 2011 02:35:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OpenJPA Auto Detach Configuration and Memory Issues</title>
		<link>http://robin.mytechtip.com/2010/12/06/openjpa-auto-detach-memory-issues/</link>
		<comments>http://robin.mytechtip.com/2010/12/06/openjpa-auto-detach-memory-issues/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 06:10:09 +0000</pubDate>
		<dc:creator>robin</dc:creator>
				<category><![CDATA[configuration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[EntityManager]]></category>
		<category><![CDATA[memory leaking]]></category>
		<category><![CDATA[openjpa]]></category>

		<guid isPermaLink="false">http://robin.mytechtip.com/?p=149</guid>
		<description><![CDATA[When I was doing some work with OpenJPA, I found there is a possible memory leaking issue when OpenJPA is used for data access without the proper configuration of the parameter of auto detach (openjpa.AutoDetach).
The scenario that the memory issue may happen:

 One EntityManagerFactory and One EntityManager are used throughout the whole session;
The session contains [...]]]></description>
			<content:encoded><![CDATA[<p>When I was doing some work with OpenJPA, I found there is a possible memory leaking issue when OpenJPA is used for data access without the proper configuration of the parameter of auto detach (<a href="http://openjpa.apache.org/builds/2.0.1/apache-openjpa-2.0.1/docs/manual/ref_guide_conf_openjpa.html#openjpa.AutoDetach" onclick="javascript:pageTracker._trackPageview('/outbound/article/openjpa.apache.org');">openjpa.AutoDetach</a>).</p>
<p>The scenario that the memory issue may happen:</p>
<ul>
<li> One <code>EntityManagerFactory </code>and One <code>EntityManager </code>are used throughout the whole session;</li>
<li>The session contains a lot of queries, they are all read-only queries;</li>
<li> Most queries are to retrieve database records based on primary keys;</li>
<li>The records retrieved are approximately evenly distributed across primary keys;</li>
<li> The database being queried has a large amount of data.</li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-3371750151985752";
google_ad_slot = "0950801022";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br />
It seems that OpenJPA keeps references to data retrieved from the queries as cache. This prevents JVM garbage collecting them. Eventually, out of memory error happens.</p>
<p>This time, it seems that <a href="http://robin.mytechtip.com/2010/03/24/remember-to-clear-entity-manager-if-entities-no-longer-used/" >the call to <code>clear </code>method on <code>EntityManager </code></a>does not even work. Finally, we added the following properties and it seemed to solve the issue:</p>
<blockquote><p><code>openjpa.AutoDetach=nontx-read</code></p></blockquote>
<p>In fact, it is not a good practice to use <code>EntityManager </code>as shown in the scenario. Maybe the initial consideration is to re-use the single <code>EntityManager </code>as much as possible to help improve the performance of the session that is created in a batch process.</p>
<p>In general, the <code>EntityManager </code>object should not live in the whole long session. Instead it should be created on demand and closed and disregarded when finishing its unit of work. However, i haven&#8217;t got time to test how worse the performance can degrade if it is implemented this way.</p>
]]></content:encoded>
			<wfw:commentRss>http://robin.mytechtip.com/2010/12/06/openjpa-auto-detach-memory-issues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Configure Spring beans.xml File: Collection properties and EntityManagerFactory Examples</title>
		<link>http://robin.mytechtip.com/2010/06/07/configure-spring-beans-xml-collection-properties-and-entitymanagerfactory-examples/</link>
		<comments>http://robin.mytechtip.com/2010/06/07/configure-spring-beans-xml-collection-properties-and-entitymanagerfactory-examples/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 05:43:51 +0000</pubDate>
		<dc:creator>robin</dc:creator>
				<category><![CDATA[configuration]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[beans.xml]]></category>
		<category><![CDATA[EntityManager]]></category>
		<category><![CDATA[openjpa]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://robin.mytechtip.com/?p=106</guid>
		<description><![CDATA[


Java beans in Spring can also have their collection properties populated in the beans.xml file. The collection properties here mean things like List, Map and Array.
For example, if you want to create a JPA EntityManagerFactory with a set of connection properties, these properties can be configured in beans.xml like this:

&#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62;
&#60;beans xmlns=&#34;http://www.springframework.org/schema/beans&#34;
  xmlns:xsi=&#34;http://www.w3.org/2001/XMLSchema-instance&#34; [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-3371750151985752";
google_ad_slot = "0950801022";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
</p>
<p>Java beans in Spring can also have their collection properties populated in the beans.xml file. The collection properties here mean things like List, Map and Array.</p>
<p>For example, if you want to create a JPA EntityManagerFactory with a set of connection properties, these properties can be configured in beans.xml like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span> </span>
<span style="color: #009900;">  <span style="color: #000066;">xmlns:p</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/p&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;entityManagerFactoryCreator&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.mytechtip.example.EntityManagerFactoryCreator&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;persistenceUnitName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;HRMN_Repository&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jpaProperties&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;openjpa.ConnectionURL&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>the_url<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;openjpa.ConnectionDriverName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>the_driver_class_name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;openjpa.ConnectionUserName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>the_user_name<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;openjpa.ConnectionPassword&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>the_password<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Here, jpaProperties in the actual java class &#8220;<code>EntityManagerFactoryCreator</code>&#8221; is an instance of <code>Properties</code>. And you can just use this properties to create EntityManagerFactory.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Properties</span> jpaProperties<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//...</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setJpaProperties<span style="color: #009900;">&#40;</span><span style="color: #003399;">Properties</span> prop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jpaProperties</span> <span style="color: #339933;">=</span> prop<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #000000; font-weight: bold;">public</span> EntityManagerFactory create<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> Persistence.<span style="color: #006633;">createEntityManagerFactory</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myPersistenceUnit&quot;</span>, prop<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Actually, Spring already have something similar for you to access EntityManagerFactory with ease. The beans used for the task are: </p>
<ul>
<li><code>org.springframework.orm.jpa.LocalEntityManagerFactoryBean</code></li>
<li><code>org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean</code></li>
</ul>
<p>More information / examples can be found <a href="http://static.springsource.org/spring/docs/2.5.x/reference/orm.html#orm-jpa-setup" onclick="javascript:pageTracker._trackPageview('/outbound/article/static.springsource.org');">here about the support of JPA entity manager factory creatation in Spring</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://robin.mytechtip.com/2010/06/07/configure-spring-beans-xml-collection-properties-and-entitymanagerfactory-examples/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use OpenJPA with MySQL: Some Tips</title>
		<link>http://robin.mytechtip.com/2009/10/14/use-openjpa-with-mysql-some-tips/</link>
		<comments>http://robin.mytechtip.com/2009/10/14/use-openjpa-with-mysql-some-tips/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 03:02:01 +0000</pubDate>
		<dc:creator>robin</dc:creator>
				<category><![CDATA[configuration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javadb]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[openjpa]]></category>

		<guid isPermaLink="false">http://robin.mytechtip.com/?p=51</guid>
		<description><![CDATA[


OpenJPA is an implementation of the JPA (Java Persistence API) standards. Initially, I used OpenJPA with Derby which is also known as JavaDB. It works well. However, when i tried to use MySQL as the backend database, I encountered a few issues. Here are a few small tips i would like to share when using [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-3371750151985752";
google_ad_slot = "0950801022";
google_ad_width = 468;
google_ad_height = 60;
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<br />
OpenJPA is an implementation of the JPA (Java Persistence API) standards. Initially, I used OpenJPA with Derby which is also known as JavaDB. It works well. However, when i tried to use MySQL as the backend database, I encountered a few issues. Here are a few small tips i would like to share when using OpenJPA with MySQL:</p>
<p><strong>Change the default storage engine</strong></p>
<p>By default, OpenJPA uses innodb as the storage engine. While my application does not need to support transactions and other fancy db features, i&#8217;d like to use more plain and &#8220;traditional&#8221; myisam storage engine. To do so, I just need to set the property &#8220;openjpa.jdbc.DBDictionary&#8221; with the value &#8220;TableType=myisam&#8221;. This property object is the one passed to the method &#8220;Persistence.createEntityManagerFactory(String, Properties)&#8221;.</p>
<p><strong>Allow to store bigger objects (blobs)</strong><br />
By default, OpenJPA maps the java object to mysql data type &#8220;blob&#8221;. However, as you may be aware of, blob type in mysql only can store up to 65536(2<sup>16</sup>) bytes of data (check http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html), which is quite smaller than the blob in JavaDB or Derby (2G).</p>
<p>Mysql does provide other data types which supports larger storage. mediumblob supports up to 16M while longblob up to 4G. So if I want to store objects other than 64K, I need to change the mapping in OpenJPA. To change the mapping, just set the same property &#8220;openjpa.jdbc.DBDictionary&#8221;, but with value &#8220;blobTypeName=mediumblob&#8221;.</p>
<p>If I want both myisam as storage engine and mediumblob as mapped blob type, the property should be set as: &#8220;TableType=myisam,blobTypeName=mediumblob&#8221;.</p>
<p><strong>Avoid the error &#8220;Packet too large&#8221;</strong><br />
Now i can save objects up to 16M in size to the database. I run my program, only to find another exception popped up. It says something like &#8220;Packet too large&#8221;.</p>
<p>This requires the change of mysql server setting as indicated in <a id="h6su" title="mysql documentation" href="http://dev.mysql.com/doc/refman/5.0/en/packet-too-large.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/dev.mysql.com');">mysql documentation</a>. The setting &#8220;<a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet" onclick="javascript:pageTracker._trackPageview('/outbound/article/dev.mysql.com');"><span style="font-family: Courier New">max_allowed_packet</span></a>&#8220;, default to 1M bytes, is too small in my case.</p>
]]></content:encoded>
			<wfw:commentRss>http://robin.mytechtip.com/2009/10/14/use-openjpa-with-mysql-some-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

