/hazelcast-documentation/src/main/docbook/manual/content/HibernateSecondLevelCache.xml

https://bitbucket.org/gabral6_gmailcom/hazelcast · XML · 301 lines · 277 code · 9 blank · 15 comment · 0 complexity · 04c474eb4cc9de0531f29a7940d41e83 MD5 · raw file

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <!--
  3. ~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  4. ~
  5. ~ Licensed under the Apache License, Version 2.0 (the "License");
  6. ~ you may not use this file except in compliance with the License.
  7. ~ You may obtain a copy of the License at
  8. ~
  9. ~ http://www.apache.org/licenses/LICENSE-2.0
  10. ~
  11. ~ Unless required by applicable law or agreed to in writing, software
  12. ~ distributed under the License is distributed on an "AS IS" BASIS,
  13. ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ~ See the License for the specific language governing permissions and
  15. ~ limitations under the License.
  16. -->
  17. <simplesect version="5.0" xmlns="http://docbook.org/ns/docbook"
  18. xmlns:xi="http://www.w3.org/2001/XInclude"
  19. xmlns:xlink="http://www.w3.org/1999/xlink"
  20. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  21. xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
  22. http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd">
  23. <para>
  24. Hazelcast provides distributed second level cache for
  25. your Hibernate entities, collections and queries. Hazelcast has two implementations of Hibernate
  26. 2nd level cache, one for hibernate-pre-3.3 and one for hibernate-3.3.x versions. In your
  27. Hibernate configuration file (ex: hibernate.cfg.xml), add these properties:
  28. <itemizedlist>
  29. <listitem>
  30. <para>To enable use of second level cache
  31. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.use_second_level_cache">true</property>
  32. ]]></programlisting>
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>To enable use of query cache
  37. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.use_query_cache">true</property>
  38. ]]></programlisting>
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>And to force minimal puts into cache
  43. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.use_minimal_puts">true</property>
  44. ]]></programlisting>
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>To configure Hazelcast for Hibernate, it is enough to put configuration file named
  49. <literal>hazelcast.xml</literal>
  50. into root of your classpath. If Hazelcast can
  51. not find
  52. <literal>hazelcast.xml</literal>
  53. then it will use default configuration
  54. from hazelcast.jar.
  55. </para>
  56. </listitem>
  57. <listitem>
  58. <para>You can define custom named Hazelcast configuration xml file with one of these
  59. Hibernate configuration properties.
  60. <programlisting language="xml">
  61. <![CDATA[<property name="hibernate.cache.provider_configuration_file_resource_path">
  62. hazelcast-custom-config.xml
  63. </property>]]>
  64. </programlisting>
  65. or
  66. <programlisting language="xml">
  67. <![CDATA[<property name="hibernate.cache.hazelcast.configuration_file_path">
  68. hazelcast-custom-config.xml
  69. </property>]]>
  70. </programlisting>
  71. </para>
  72. </listitem>
  73. <listitem>
  74. <para>You can set up Hazelcast to connect cluster as LiteMember. LiteMember is a
  75. member of the cluster, it has socket connection to every member in the cluster and
  76. it knows where the data, but does not contain any data.
  77. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.hazelcast.use_lite_member">true</property>
  78. ]]></programlisting>
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>You can set up Hazelcast to connect cluster as Native Client. Native client is not
  83. member and it connects to one of the cluster members and delegates all cluster wide
  84. operations to it. When the relied cluster member dies, client will transparently
  85. switch to another live member. <emphasis role="italic">(Native Client property takes precedence
  86. over LiteMember property.)</emphasis>
  87. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.hazelcast.use_native_client">true</property>
  88. ]]></programlisting>
  89. To setup Native Client properly, you should add Hazelcast
  90. <emphasis role="bold">group-name</emphasis>,
  91. <emphasis role="bold">group-password</emphasis> and
  92. <emphasis role="bold">cluster member address</emphasis> properties.
  93. Native Client
  94. will connect to defined member and will get addresses of all members in the cluster.
  95. If the connected member will die or leave the cluster, client will automatically
  96. switch to another member in the cluster.
  97. <programlisting language="xml"><![CDATA[<property name="hibernate.cache.hazelcast.native_client_address">10.34.22.15</property>
  98. <property name="hibernate.cache.hazelcast.native_client_group">dev</property>
  99. <property name="hibernate.cache.hazelcast.native_client_password">dev-pass</property>
  100. ]]></programlisting>
  101. <emphasis role="italic">To use Native Client you should add
  102. <literal>hazelcast-client-&lt;version&gt;.jar</literal>
  103. into your
  104. classpath.
  105. </emphasis>
  106. </para>
  107. <para>
  108. <link xlink:href="#NativeClient">Read more about NativeClient &amp; LiteMember</link>
  109. </para>
  110. </listitem>
  111. <listitem>
  112. <para>If you are using one of Hibernate pre-3.3 version, add following property.
  113. <programlisting language="xml">
  114. <![CDATA[<property name="hibernate.cache.provider_class">
  115. com.hazelcast.hibernate.provider.HazelcastCacheProvider
  116. </property>]]>
  117. </programlisting>
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para>If you are using Hibernate 3.3.x (or newer) version, you can choose to use either
  122. configuration property above (Hibernate has a built-in bridge to use old-style cache
  123. implementations) or following property.
  124. <programlisting language="xml">
  125. <![CDATA[<property name="hibernate.cache.region.factory_class">
  126. com.hazelcast.hibernate.HazelcastCacheRegionFactory
  127. </property>]]>
  128. </programlisting>
  129. Or as an alternative you can use <code>HazelcastLocalCacheRegionFactory</code>
  130. which stores data in local node and sends invalidation messages when an entry is updated on local.
  131. <programlisting language="xml">
  132. <![CDATA[<property name="hibernate.cache.region.factory_class">
  133. com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory
  134. </property>]]>
  135. </programlisting>
  136. </para>
  137. </listitem>
  138. </itemizedlist>
  139. Hazelcast creates a separate distributed map for each Hibernate cache region. So
  140. these regions can be configured easily via Hazelcast map configuration. You can define
  141. <emphasis role="bold">backup</emphasis>,
  142. <emphasis role="bold">eviction</emphasis>,
  143. <emphasis role="bold">TTL</emphasis> and
  144. <emphasis role="bold">Near Cache</emphasis> properties.
  145. <itemizedlist>
  146. <listitem>
  147. <para>
  148. <link xlink:href="#MapBackup">Backup Configuration</link>
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. <link xlink:href="#MapEviction">Eviction And TTL Configuration</link>
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <link xlink:href="#MapNearCache">Near Cache Configuration</link>
  159. </para>
  160. </listitem>
  161. </itemizedlist>
  162. Hibernate has four cache concurrency strategies:
  163. <emphasis>read-only</emphasis>,
  164. <emphasis>read-write</emphasis>,
  165. <emphasis>nonstrict-read-write</emphasis> and
  166. <emphasis>transactional</emphasis>.
  167. But Hibernate does not forces cache providers to support all strategies. And Hazelcast supports
  168. first three (<emphasis role="bold">read-only</emphasis>,
  169. <emphasis role="bold">read-write</emphasis>,
  170. <emphasis role="bold">nonstrict-read-write</emphasis>) of these four strategies.
  171. Hazelcast has no support for<emphasis>transactional</emphasis> strategy yet.
  172. <itemizedlist>
  173. <listitem>
  174. <para>If you are using xml based class configurations, you should add a
  175. <emphasis>cache</emphasis> element into your configuration with
  176. <emphasis>usage</emphasis> attribute with one of
  177. <emphasis>read-only</emphasis>,
  178. <emphasis>read-write</emphasis>,
  179. <emphasis>nonstrict-read-write</emphasis>.
  180. <programlisting language="xml"><![CDATA[<class name="eg.Immutable" mutable="false">
  181. <cache usage="read-only"/>
  182. ....
  183. </class>
  184. <class name="eg.Cat" .... >
  185. <cache usage="read-write"/>
  186. ....
  187. <set name="kittens" ... >
  188. <cache usage="read-write"/>
  189. ....
  190. </set>
  191. </class>
  192. ]]></programlisting>
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>If you are using Hibernate-Annotations then you can add
  197. <emphasis>class-cache</emphasis> or
  198. <emphasis>collection-cache</emphasis> element into your Hibernate configuration file with
  199. <emphasis>usage</emphasis> attribute with one of
  200. <emphasis>read only</emphasis>,
  201. <emphasis>read/write</emphasis>,
  202. <emphasis>nonstrict read/write</emphasis>.
  203. <programlisting language="xml"><![CDATA[<class-cache usage="read-only" class="eg.Immutable"/>
  204. <class-cache usage="read-write" class="eg.Cat"/>
  205. <collection-cache collection="eg.Cat.kittens" usage="read-write"/>
  206. ]]></programlisting>
  207. </para>
  208. </listitem>
  209. </itemizedlist>
  210. OR
  211. <itemizedlist>
  212. <listitem>
  213. <para>Alternatively, you can put Hibernate Annotation's
  214. <emphasis>@Cache</emphasis>
  215. annotation on your entities and collections.
  216. <programlisting language="java"><![CDATA[@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
  217. public class Cat implements Serializable {
  218. ...
  219. }
  220. ]]></programlisting>
  221. </para>
  222. </listitem>
  223. </itemizedlist>
  224. The last thing you should be aware of is to drop
  225. hazelcast-hibernate-&lt;version&gt;.jar into your classpath.
  226. <itemizedlist>
  227. <title>Additional Properties:</title>
  228. <listitem>
  229. <para>
  230. <emphasis role="bold">Accessing underlying
  231. <code>HazelcastInstance</code>
  232. </emphasis>
  233. </para>
  234. <para>Using
  235. <code>com.hazelcast.hibernate.instance.HazelcastAccessor</code>
  236. you can access the underlying
  237. <code>HazelcastInstance</code>
  238. used by Hibernate SessionFactory.
  239. <programlisting language="java"><![CDATA[SessionFactory sessionFactory = ...;
  240. HazelcastInstance hazelcastInstance = HazelcastAccessor.getHazelcastInstance(sessionFactory);
  241. ]]></programlisting>
  242. </para>
  243. </listitem>
  244. <listitem>
  245. <para>
  246. <emphasis role="bold">Changing/setting lock timeout value of
  247. <emphasis role="italic">read-write</emphasis>
  248. strategy
  249. </emphasis>
  250. </para>
  251. <para>Lock timeout value can be set using
  252. <literal>hibernate.cache.hazelcast.lock_timeout</literal>
  253. Hibernate property.
  254. Value should be in milliseconds and default value is 10000 ms (10 seconds).
  255. </para>
  256. </listitem>
  257. <listitem>
  258. <para>
  259. <emphasis role="bold">Using named
  260. <literal>HazelcastInstance</literal>
  261. </emphasis>
  262. </para>
  263. <para>
  264. Instead of creating a new <literal>HazelcastInstance</literal> for each <literal>SessionFactory</literal>,
  265. an existing instance can be used by setting
  266. <literal>hibernate.cache.hazelcast.instance_name</literal>
  267. Hibernate property to <literal>HazelcastInstance</literal>'s name.
  268. For more information see <link xlink:href="#NamedHazelcastInstance">Named HazelcastInstance</link>.
  269. </para>
  270. </listitem>
  271. <listitem>
  272. <para>
  273. <emphasis role="bold">Disabling shutdown during SessionFactory.close()</emphasis>
  274. </para>
  275. <para>Shutting down
  276. <literal>HazelcastInstance</literal>
  277. can be disabled during
  278. <code>SessionFactory.close()</code>
  279. by setting
  280. <literal>hibernate.cache.hazelcast.shutdown_on_session_factory_close</literal>
  281. Hibernate property to false.
  282. <emphasis role="italic">(In this case Hazelcast property
  283. <literal>hazelcast.shutdownhook.enabled</literal>
  284. should not
  285. be set to false.)
  286. </emphasis>
  287. Default value is <literal>true</literal>.
  288. </para>
  289. </listitem>
  290. </itemizedlist>
  291. </para>
  292. </simplesect>