<?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>Mike Desjardins&#039; Series of Tubes &#187; cache</title>
	<atom:link href="http://www.mikedesjardins.net/content/category/cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikedesjardins.net/content</link>
	<description>freelance software developer consultant in portland, maine</description>
	<lastBuildDate>Wed, 02 Feb 2011 00:14:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Clearing Hibernate Second-Level Caches</title>
		<link>http://www.mikedesjardins.net/content/2008/10/clearing-hibernate-second-level-caches/</link>
		<comments>http://www.mikedesjardins.net/content/2008/10/clearing-hibernate-second-level-caches/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 12:03:00 +0000</pubDate>
		<dc:creator>Mike Desjardins</dc:creator>
				<category><![CDATA[cache]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jpa]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/?p=34</guid>
		<description><![CDATA[Recently, I wanted to be able to clear out all of the Hibernate caches via JBoss&#8217;s JMX console. I could have taken the easy way out; we&#8217;re using EHCache, so it could have been as simple as calling CacheManager.clearAll(). However, that would have tied me to a specific cache provider. We&#8217;re still evaluating switching to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://mikedesjardins.us/blog/uploaded_images/chalkboards-1-FolkeB-735126.jpg" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer;" src="http://mikedesjardins.us/blog/uploaded_images/chalkboards-1-FolkeB-735124.jpg" border="0" alt="" /></a>Recently, I wanted to be able to clear out all of the Hibernate caches via JBoss&#8217;s JMX console.  I could have taken the easy way out; we&#8217;re using EHCache, so it could have been as simple as calling <span style="font-style: italic;">CacheManager.clearAll()</span>.  However, that would have tied me to a specific cache provider.  We&#8217;re still evaluating switching to other cache providers.  Ideally, my solution would not be dependent on a specific cache implementation.</p>
<p>Hibernate&#8217;s API does provide a simple way to clear specific caches, but does not provide any method for clearing out all of them.  Writing your own is fairly straightforward.  First, you obtain all of the entity and collection metadata from the session factory.  Next you iterate over the entities, and if the object is cached, you clear out all of the caches associated with the persisted class or collection.  Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  @PersistenceContext
  <span style="color: #000000; font-weight: bold;">private</span> EntityManager em<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearHibernateCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>      
    Session s <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Session<span style="color: #009900;">&#41;</span>em.<span style="color: #006633;">getDelegate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    SessionFactory sf <span style="color: #339933;">=</span> s.<span style="color: #006633;">getSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Map<span style="color: #339933;">&lt;</span>String,EntityPersister<span style="color: #339933;">&gt;</span> classMetadata <span style="color: #339933;">=</span> sf.<span style="color: #006633;">getAllClassMetadata</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>EntityPersister ep <span style="color: #339933;">:</span> classMetadata.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>          
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>ep.<span style="color: #006633;">hasCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>              
        sf.<span style="color: #006633;">evictEntity</span><span style="color: #009900;">&#40;</span>ep.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRegionName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          
      <span style="color: #009900;">&#125;</span>      
    <span style="color: #009900;">&#125;</span>
&nbsp;
    Map<span style="color: #339933;">&lt;</span>String,AbstractCollectionPersister<span style="color: #339933;">&gt;</span> collMetadata <span style="color: #339933;">=</span> sf.<span style="color: #006633;">getAllCollectionMetadata</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>AbstractCollectionPersister acp <span style="color: #339933;">:</span> collMetadata.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>          
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>acp.<span style="color: #006633;">hasCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>              
        sf.<span style="color: #006633;">evictCollection</span><span style="color: #009900;">&#40;</span>acp.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRegionName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          
      <span style="color: #009900;">&#125;</span>      
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>  
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, if we decide to switch to a different cache provider, this code will not need to be re-written.  Hopefully we won&#8217;t ever change to a different JPA implementaion.  <img src='http://www.mikedesjardins.net/content/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span style="font-style: italic;">Photo Credit: </span><a style="font-style: italic;" href="http://flickr.com/people/24337020@N07/">FolkeB</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mikedesjardins.net/content/2008/10/clearing-hibernate-second-level-caches/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

