{"id":644,"date":"2010-09-23T22:47:02","date_gmt":"2010-09-24T04:47:02","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=644"},"modified":"2010-09-23T22:47:02","modified_gmt":"2010-09-24T04:47:02","slug":"turning-on-hibernate-query-caching","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/644","title":{"rendered":"Turning on hibernate query caching"},"content":{"rendered":"<p>Sometimes I write a blog post because I want to say something to others, other times because I want to help others, and sometimes I write one for myself.\u00a0 This is one of the latter type of posts.<\/p>\n<p>I find that nothing drives home a lesson I learned like writing out the solution on my blog, and more than one time I&#8217;ve searched my blog because I remember I had written about a topic of current interest in the past.<\/p>\n<p>If you want hibernate, a java ORM tool, to cache your query results, there&#8217;s <a href=\"http:\/\/docs.jboss.org\/hibernate\/stable\/core\/reference\/en\/html\/performance.html#performance-querycache\">a section in the hibernate manual<\/a>. Like most of the hibernate documentation, it&#8217;s quite good.\u00a0 One thing that caught me up, though, is that I thought that all queries were cached, as soon as you had set up the correct configuration in your hibernate xml files and marked your objects as cacheable.<\/p>\n<p>However, this is not the case,\u00a0 as I learned when I turned on the <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.1\/en\/query-log.html\">mysql query log<\/a> and looked at the calls the interesting web application was making.\u00a0 I did quite a bit of searching, but the answer was in the <a href=\"http:\/\/docs.jboss.org\/hibernate\/stable\/core\/reference\/en\/html\/performance.html#performance-querycache\">same section<\/a> that told me how to set up the configuration (as well as in this <a href=\"https:\/\/forum.hibernate.org\/viewtopic.php?f=1&amp;t=1006415&amp;p=2434416&amp;hilit=setCacheable#p2434416\">forum post<\/a>).<\/p>\n<blockquote><p><a id=\"performance-querycache-enable\">&#8230;most queries do not benefit from caching or their results.  So by                 default, individual queries are not cached even after enabling query caching. To enable                 results caching for a particular query, call                 <code>org.hibernate.Query.setCacheable(true)<\/code>.<\/a><\/p><\/blockquote>\n<p>Doh!\u00a0 Following these instructions decreased the page load time for some heavy query pages from 8 seconds to 0.5 seconds (the second time the page was loaded).\u00a0 Impressive indeed, though the production environment might not see such drastic improvements.<\/p>\n<p>One additional note&#8211;you can use query caching for SQLQueries (like group operations) by telling hibernate what the type is of each returned value.\u00a0 If you don&#8217;t use addScalar, but you do mark a SQLQuery as cacheable, you&#8217;ll get a java.lang.ArrayIndexOutOfBoundsException when that code executes.\u00a0 <a href=\"https:\/\/forum.hibernate.org\/viewtopic.php?p=2373167\">More here<\/a>.<\/p>\n<p>These articles were helpful to me as I navigated Hibernate caching, above and beyond the reference documentation:<\/p>\n<ul>\n<li><a href=\"http:\/\/acupof.blogspot.com\/2008\/01\/background-hibernate-comes-with-three.html\">http:\/\/acupof.blogspot.com\/2008\/01\/background-hibernate-comes-with-three.html<\/a><\/li>\n<li><a href=\"http:\/\/tech.puredanger.com\/2009\/07\/10\/hibernate-query-cache\/\">http:\/\/tech.puredanger.com\/2009\/07\/10\/hibernate-query-cache\/<\/a><\/li>\n<li><a href=\"http:\/\/blog.dynatrace.com\/2009\/02\/16\/understanding-caching-in-hibernate-part-two-the-query-cache\/\">http:\/\/blog.dynatrace.com\/2009\/02\/16\/understanding-caching-in-hibernate-part-two-the-query-cache\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes I write a blog post because I want to say something to others, other times because I want to help others, and sometimes I write one for myself.\u00a0 This [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,28],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-java","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":2,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":646,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/644\/revisions\/646"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}