<?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>Technotrance, Illusions and Perspectives &#187; query-cache</title>
	<atom:link href="http://myblog.shriharisc.com/tag/query-cache/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Quick note about Hibernate Caching</title>
		<link>http://myblog.shriharisc.com/2009/09/14/a-quick-note-about-hibernate-caching/</link>
		<comments>http://myblog.shriharisc.com/2009/09/14/a-quick-note-about-hibernate-caching/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 16:32:43 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[java-internals]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[query-cache]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=111</guid>
		<description><![CDATA[1)    First Level Cache (Transaction Layer)
a) Associated with a Hibernate Session (transaction scoped) and this cache is used by Hibernate transparently. Hibernate requires a key to load object from the session cache. Hence its better to call a load() or get(), when the key(EntityKey) is known, rather than using a HQL query.
b) All queries (state [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-right:0;text-align:left;" dir="ltr"><span style="text-decoration:underline;"><strong><em>1)    First Level Cache (Transaction Layer)</em></strong></span></p>
<p style="margin-right:0;text-align:left;" dir="ltr">a) Associated with a Hibernate Session (transaction scoped) and this cache is used by Hibernate transparently. Hibernate requires a key to load object from the session cache. Hence its better to call a load() or get(), when the key(EntityKey) is known, rather than using a HQL query.<br />
b) All queries (state of an entity, updates) in the cache gets flushed / committed to the database only when  Transaction context ends (i.e.) <strong>getTransaction().commit</strong> is called.<br />
c) Always used and cannot be turned off.</p>
<p style="margin-right:0;text-align:left;" dir="ltr"><span style="text-decoration:underline;"><strong><em>2)    Second Level Cache (Application Layer)</em></strong></span></p>
<p style="margin-right:0;text-align:left;" dir="ltr">a) Associated with a Hibernate&#8217;s SessionFactory (process scoped). Enabled by default in Hibernate 3 and uses <strong>EHCache</strong> as default cache provider.<br />
b) The cache entries are dehydrated states of entity objects. A dehydrated state of entity refers to a key­-value  pair where key is the entity id (of the dehydrated entity) and the set of (deep copy) of attribute­values  (dehydrated entity fields) refer to the value.  Hibernate does not cache associations in a dehydrated object by default. One has to manually configured which association in a dehydratable entity to be cached.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">Syntax : { id ­&gt; { atribute1, attribute2, attribute3,{attribute41, attribute42},&#8230; } }</p>
<p style="margin-right:0;text-align:left;" dir="ltr"><span style="text-decoration:underline;"><strong><em>3)   Query cache (</em></strong><strong>org.hibernate.cache.QueryCache)</strong></span></p>
<p style="margin-right:0;text-align:left;" dir="ltr">a) Defines 2 cache regions : <strong>org.hibernate.cache.StandardQueryCache</strong> (stores query with parameters as cache key) and <strong>org.hibernate.cache.UpdateTimestampsCache</strong> (keeps stale query results of last results fetch). Both the cache regions are evicted when the entity related to the cacheable query is updated.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">Syntax : { query,{parameters}} &#8212;&gt; {id of cached entity}</p>
<p style="margin-right:0;text-align:left;" dir="ltr">b) A specific query results are cached, by specifying setCacheable(true) on Session&#8217;s Query handle.</p>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:0;width:1px;height:1px;">
<p style="margin-right:0;text-align:left;" dir="ltr"><em>1)    First Level Cache (Transaction Layer)</em></p>
<p style="margin-right:0;text-align:left;" dir="ltr">a) Associated with a Hibernate Session (transaction scoped) and this cache is used by Hibernate transparently. Hibernate requires a key to load object from the session cache. Hence its better to call a load() or get(), when the key(EntityKey) is known, rather than using a HQL query.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">b) All queries (state of an entity, updates) in the cache gets flushed / committed to the database only when  Transaction context ends (i.e.) <strong>getTransaction().commit</strong> is called.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">c) Always used and cannot be turned off.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">
<p style="margin-right:0;text-align:left;" dir="ltr"><em>2)    Second Level Cache (Application Layer)</em></p>
<p style="margin-right:0;text-align:left;" dir="ltr">a) Associated with a Hibernate&#8217;s SessionFactory (process scoped). Enabled by default in Hibernate 3 and uses <strong>EHCache</strong> as default cache provider.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">
<p style="margin-right:0;text-align:left;" dir="ltr">b) The cache entries are dehydrated states of entity objects. A dehydrated state of entity refers to a key­-value  pair where key is the entity id (of the dehydrated entity) and the set of (deep copy) of attribute­values  (dehydrated entity fields) refer to the value.  Hibernate does not cache associations in a dehydrated object by default. One has to manually configured which association in a dehydratable entity to be cached.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">
<p style="margin-right:0;text-align:left;" dir="ltr">Syntax : { id ­&gt; { atribute1, attribute2, attribute3,{attribute41, attribute42},&#8230; } }</p>
<p style="margin-right:0;text-align:left;" dir="ltr">
<p style="margin-right:0;text-align:left;" dir="ltr"><em>3)   Query cache (</em><strong>org.hibernate.cache.QueryCache</strong>)</p>
<p style="margin-right:0;text-align:left;" dir="ltr">
<p style="margin-right:0;text-align:left;" dir="ltr">a) Defines 2 cache regions : <strong>org.hibernate.cache.StandardQueryCache</strong> (stores query with parameters as cache key) and <strong>org.hibernate.cache.UpdateTimestampsCache</strong> (keeps stale query results of last results fetch). Both the cache regions are evicted when the entity related to the cacheable query is updated.</p>
<p style="margin-right:0;text-align:left;" dir="ltr">Syntax : { query,{parameters}} &#8212;&gt; {id of cached entity}</p>
<p style="margin-right:0;text-align:left;" dir="ltr">b) A specific query results are cached, by specifying setCacheable(true) on session&#8217;s Query handle.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/09/14/a-quick-note-about-hibernate-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Overcoming pitfalls with Query cache in Hibernate</title>
		<link>http://myblog.shriharisc.com/2009/06/27/overcoming-pitfalls-with-query-cache-in-hibernate/</link>
		<comments>http://myblog.shriharisc.com/2009/06/27/overcoming-pitfalls-with-query-cache-in-hibernate/#comments</comments>
		<pubDate>Sat, 27 Jun 2009 17:41:43 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[query-cache]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=44</guid>
		<description><![CDATA[Query caching in Hibernate used to cache query state and results, has  multiple shortcomings if, not used judiciously. Some of the common pitfalls include:
(i) Query Cache structural representation is a QueryKey mapped to Object[][] (2-dimensional)  and holds majority of persistent object references. QueryKey represents query specific data such as the SQL Query text with parameters [...]]]></description>
			<content:encoded><![CDATA[<p>Query caching in Hibernate used to cache query state and results, has  multiple shortcomings if, not used judiciously. Some of the common pitfalls include:</p>
<p>(i) Query Cache structural representation is a QueryKey mapped to Object[][] (2-dimensional)  and holds majority of persistent object references. QueryKey represents query specific data such as the SQL Query text with parameters (positional/named). QueryKey representation would get deteriorated if parameters represent entity object or a deep rooted object hierarchy.</p>
<p>(ii) If multiple equivalents of load() is invoked,  there could potentially create multiple parameter versions of QueryKey containing equivalent duplicate entity objects exist in the cache.</p>
<p>Following are the patterns and practices that could be followed to fix these shortcomings:</p>
<p>1) Decorate <strong>org.hibernate.cache.StandardQueryCache </strong>and override the put() method to check if a canonical equivalent of a query results object already exist in the Object[][], and assign the same QueryKey if it exists. One needs to implement <strong>org.hibernate.cache.QueryCacheFactory</strong> also to plug into the Hibernate config.</p>
<p>2) Refactor the code to set entity&#8217;s keys as query parameters, rather than setting the entire entity object. Critreia representations should also use identifiers as parameters.</p>
<p>3) Write HQL queries to use identifiers in any substitutable parameters such as WHERE clause, IN clause etc.</p>
<p>4) Use positional parameters over named parameters as it could prevent using string pools.</p>
<p>5) If you are in a single JVM using in memory cache only, use <em><span style="font-family:Courier New;">hibernate.cache.use_structured_entries=false</span></em> in your hibernate configuration.</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/06/27/overcoming-pitfalls-with-query-cache-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
