/hazelcast-documentation/src/main/docbook/manual/content/misc/CommonGotchas.xml

https://bitbucket.org/gabral6_gmailcom/hazelcast · XML · 101 lines · 83 code · 3 blank · 15 comment · 0 complexity · 01ddae41f1d0b45056e5ceb2bc9146d6 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. <sect1 xml:id="CommonGotchas" 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. <title>Common Gotchas</title>
  24. <para>Hazelcast is the distributed implementation of several structures that exist in Java.
  25. Most of the time it behaves as you expect. However there are some design choices in
  26. Hazelcast that violate some contracts. This page will list those violations.
  27. <orderedlist>
  28. <listitem>
  29. <para>
  30. <emphasis role="bold">equals() and hashCode() methods for the objects stored
  31. in Hazelcast
  32. </emphasis>
  33. </para>
  34. <para>When you store a key, value in a distributed Map, Hazelcast serializes the key
  35. and value and stores the byte array version of them in local ConcurrentHashMaps.
  36. And this ConcurrentHashMap uses the equals and hashCode methods of byte array
  37. version of your key. So it does not take into account the actual equals and
  38. hashCode implementations of your objects. So it is important that you choose
  39. your keys in a proper way. Implementing the equals and hashCode is not enough,
  40. it is also important that the object is always serialized into the same byte
  41. array. All primitive types, like; String, Long, Integer and etc. are good
  42. candidates for keys to use in Hazelcast. An unsorted Set is an example of a very
  43. bad candidate because Java Serialization may serialize the same unsorted set in
  44. two different byte arrays.
  45. </para>
  46. <para>Note that the distributed Set and List stores its entries as the keys in a
  47. distributed Map. So the notes above apply to the objects you store in Set and
  48. List.
  49. </para>
  50. </listitem>
  51. <listitem>
  52. <para>
  53. Hazelcast always return a clone copy of a value. Modifying the returned value does not change
  54. the actual value in the map (or multimap or list or set).
  55. You should put modified value back to make changes visible to all nodes.
  56. <programlisting language="java"><![CDATA[
  57. V value = map.get(key);
  58. value.updateSomeProperty();
  59. map.put(key, value);
  60. ]]></programlisting>
  61. If <code>cache-value</code> is true (default is true), Hazelcast caches that returned value
  62. for fast access in local node. Modifications done to this cached value without
  63. putting it back to map will be visible to only local node, successive <code>get</code> calls will
  64. return the same cached value.
  65. To reflect modifications to distributed map, you should put modified value back into map.
  66. </para>
  67. </listitem>
  68. <listitem>
  69. <para>
  70. Collections which return values of methods such as <code>IMap.keySet</code>, <code>IMap.values</code>,
  71. <code>IMap.entrySet</code>, <code>MultiMap.get</code>, <code>MultiMap.remove</code>,
  72. <code>IMap.keySet</code>, <code>IMap.values</code>, contain cloned values. These collections are NOT
  73. backup by related Hazelcast objects.
  74. So changes to the these are <emphasis role="bold">NOT</emphasis> reflected in the originals, and vice-versa.
  75. </para>
  76. </listitem>
  77. <listitem>
  78. <para>
  79. Most of the Hazelcast operations throw an <code>RuntimeInterruptedException</code>
  80. (which is unchecked version of <code>InterruptedException</code>)
  81. if a user thread is interrupted while waiting a response.
  82. Hazelcast uses RuntimeInterruptedException to pass InterruptedException up through interfaces
  83. that don't have InterruptedException in their signatures. Users should be able to catch and handle
  84. <code>RuntimeInterruptedException</code> in such cases as if their threads are interrupted on
  85. a blocking operation.
  86. </para>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. Some of Hazelcast operations can throw <code>ConcurrentModificationException</code> under transaction
  91. while trying to acquire a resource, although operation signatures don't define such an exception.
  92. Exception is thrown if resource can not be acquired in a specific time. Users should be able to catch
  93. and handle <code>ConcurrentModificationException</code> while they are using Hazelcast transactions.
  94. </para>
  95. </listitem>
  96. </orderedlist>
  97. </para>
  98. </sect1>