Hibernate lazy loading and Hazelcast

we use Hazelcast as a Hibernate second level cache for some time, but we recognize long delays when storing and reading data when using more than one node.

We make heavy use of @OneToMany compound objects and relationships, and to improve performance, we decided to load these linked objects or collections through the lazy Hibernate download. We also implemented DataSerializable to speed up Hazelcast serialization, as stated in the Hazelcast documentation. But registering writeData / readData methods showed us that they were not actually used!

Now it is not clear if the Hibernate proxy server (which is used for lazy loading) prevents the use of DataSerializable methods (since the proxy itself may (?) Not implement the interface) and - more importantly - if Hazelcast supports lazy loading in general - and how!

+6
caching hibernate lazy-loading second-level-cache hazelcast
source share
1 answer

Hazelcast DataSerializable cannot be used with the Hibernate L2 cache, because the stored objects in the Hazalcast cluster are not objects of your object. Hibernate uses its own data format (e.g. serialization) in L2, converts your entities, their relationships and collections into its own format, and provides its own objects (implementing java.io.Serializable) for Hazelcast. Hazelcast serializes those that use standard Java serialization and distributes between clusters.

If your classes have a complex and deep graph of objects (heavy use of linked objects and 1xn or similar relationships), this double serialization problem causes long delays.

Hazelcast has nothing to do with Hibernate lazy-loading. Hibernate already stores entities and their relationships and collection collations separately. Thus, all of them can be downloaded from Hazelcast one by one. But in your use case, if most lazily loaded relationships are always loading, then this will cause several Hazelcast remote calls instead of one. Therefore, you should carefully consider where to use lazy loading.

Another trick is to use / activate Hazelcast next to the cache if your application is mostly read-only. (Btw, if it is not, then using the L2 cache may not suit you.) This way you will save a lot of remote calls, and often the necessary data will be cached locally. Near the cache, all Hazelcast card properties are supported, such as TTL, eviction, maximum size, etc.

Hazelcast close cache documentation ...

+5
source share

All Articles