6

We are using Hibernate with JPA in a project we are currently developing. Currently running on Wildfly and future possiblity is the commercial version depending on needs.

Some alternatives for the 2nd level cache are listed in the Hibernate documentation here. There also exist other solutions such as Hazelcast. Among alternatives it seems JBoss is officially supporting an Infinispan solution.

As the case in many projects, we have some tables that will rarely change if ever at all, such as a list of cities, the statuses a project can have etc. Hence our decision to use the 2nd level cache for such cases.

  • Could you share your expertise in such a scenario? Is this a good usage case? Is there a better way to persist such data? How do you handle data that rarely or never changes?
  • Which provider did you use in the past and/or currently using as a 2nd Level Cache and why? What are the pros and cons of the solution you chose?
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Dkyc
  • 89
  • 1
  • 16

3 Answers3

5
  1. The second-level cache is a relation-data cache and not a full-blown object caching solution. So it's just the basic entity properties and the foreign keys that get saved. As for entity associations, the *-To-One might be saved in different cache regions while the *-To-Many relationships require enabling the query cache too.

    I think the 2nd level cache is much more useful when your entities actually change from time to time. The 2nd level cache entry is invalidated when an entity is being determined as dirty.

  2. But if you rarely change your data, it's probably better to use an actual object cache. The object cache will store the whole object relationship tree and it's a much better alternative if you mainly operate on entities, as opposed to free-style joining projections.

    So if your workflows revolve around entities with hierarchies then an object cache is more appropriate.

  3. If your application doesn't actually operate with entities but with various data joining projections then you should add DB indexes and make sure there's enough RAM for the DB server to serve data from memory, without hitting the disk.

Solution:

I recommend you using HazelCast, which is a high-performance in-memory data grid. HazelCast allows you to either integrate it as a 2nd level cache or you can save whole object graphs if you want it. Just follow their documentation on how you can integrate it into your current application.

Community
  • 1
  • 1
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Thanks for your input. Can you elaborate on how could I use a full-blown object caching solution as opposed to Hibernate's 2nd Level Cache and how would I integrate that? Also If you have any advise regarding specific in-memory solution for 2nd level cache I would love to hear it – Dkyc Nov 29 '14 at 19:48
1

I have used ehcache to store and cache geographical data. It is very convenient to use. Take a look at their website http://ehcache.org/ .

Jitesh
  • 232
  • 2
  • 9
1

Hibernate 2nd level cache is best suitable for data that rarely or never changes. However since hibernate provides a generalized caching provider implemented by multiple caching solutions, it limits usability of features provided by caching solutions.

As a solution I will recommend you using TayzGrid as hibernate 2nd level cache, since along with the basic caching features and expirations etc, it also provides database dependencies i.e. data in cache will be automatically invalided by cache if corresponding records in database change. You can look for further details here: http://www.alachisoft.com/tayzgrid/hibernate-second-level-cache.html

Sameer Shah
  • 1,073
  • 7
  • 12