Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: hibernate + ehcache

I'm working with a spring project using hibernate and look to implement second-level cache using ehcache. I see a number of approaches to this:

  1. spring-modules-cache which introduces the @Cacheable annotation

  2. ehcache-spring-annotations a toolset which aims to be the successor of spring-modules-cache.

  3. Hibernate cache is nicely integrated into hibernate itself to perform caching using e.g., the @Cache annotation.

  4. Programmatic cache using proxies. Annotation based config quickly becomes either limited or complex (e.g., several levels of annotation nesting)

Personally I don't think spring-modules-cache is thorough enough, hence I would probably prefer consider the more actively developed ehcache-spring-annotations. Hibernate cache though seems to be most complete implementation (e.g., both read and write cache etc).

What would motivate which toolset to use? Please share your caching experience ...

like image 219
Johan Sjöberg Avatar asked Feb 03 '23 20:02

Johan Sjöberg


2 Answers

Our project uses option 3. We apply annotation org.hibernate.annotations.Cache to entities that we cache in an Ehcache, configure Ehcache using ehcache.xml, and enable and configure the Hibernate second-level cache in hibernate.cfg.xml:

    <!-- Enable the second-level cache  -->
    <property name="hibernate.cache.provider_class">
        net.sf.ehcache.hibernate.EhCacheProvider
    </property>
    <property name="hibernate.cache.region.factory_class">
        net.sf.ehcache.hibernate.EhCacheRegionFactory
    </property>
    <property name="hibernate.cache.use_query_cache">true</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.use_structured_entries">true</property>     
    <property name="hibernate.cache.generate_statistics">true</property>

For most entities, we use cache concurrency strategy CacheConcurrencyStrategy.TRANSACTIONAL:

@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)

Our Maven project uses Hibernate 3.3.2GA and Ehcache 2.2.0:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
        <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.2.1.ga</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>3.3.2.Beta1</version>
    </dependency>
like image 66
Derek Mahar Avatar answered Feb 05 '23 08:02

Derek Mahar


Spring 3.1 has a new built-in cache abstraction. Read here.

like image 36
Bozho Avatar answered Feb 05 '23 10:02

Bozho