Setting Up the GemFire Hibernate Cache Module

Setting Up the GemFire Hibernate Cache Module

You need to edit Hibernate's hibernate.cfg.xml file in order to use the Pivotal GemFire module.

  1. Turn on L2 Cache

    In the hibernate.cfg.xml file, turn on the L2 cache:

    <property name="hibernate.cache.use_second_level_cache">true</property>
  2. Set Region Factory or Cache Provider

    Associate the region factory class with GemFireRegionFactory:

    <property name="hibernate.cache.region.factory_class">
  3. Determine Cache Usage Mode

    Determine the cache usage mode for the entities in each region. There are four types of usage modes:

    Mode Description
    Read Only This mode is used when you do not plan on modifying the data already stored in your persistent storage.
    Read/Write This mode is used when you plan to both read from and write to your data.
    Non-strict Read/Write This mode is a special read/write mode that has faster write performance; however, only use this mode if no more than one client will update content at a time.
    Transactional This mode allows for transaction-based data access.
  4. Set Cache Usage Mode

    The usage mode can either be set using the hibernate-mapping file or through Java annotations.
    • To set the mode with the hibernate-mapping file, refer to this example:

          <hibernate-mapping package="PACKAGE">
            <class name="ENTITY_NAME" ...>
              <cache usage="read-write|nonstrict-read-write|read-only"/>

      In this example, PACKAGE is the name of the entity package and ENTITY_NAME is the name of your entity. The cache usage values correspond to the cache usage modes, described above. Refer to the Hibernate documentation for further information.

    • To set the mode using annotations, your class definition should look something like this:

      import org.hibernate.annotations.Cache; 
      import org.hibernate.annotations.CacheConcurrencyStrategy; 
         region = 'REGION_NAME',
      public class MyClass implements Serializable { ... }
  5. Start Hibernate

    You are now ready to build, deploy, and run your Hibernate application, which will also launch GemFire. See the Hibernate documentation for further information about performing these actions.

  6. Verify that GemFire Started Successfully

    Similar to Hibernate, GemFire uses Simple Logging Facade for Java (SLF4J) to log messages. Upon successful startup, GemFire will log the following message:

    2010-11-15 INFO [com.gemstone.gemfire.modules.hibernate.GemFireRegionFactory] - 
        <Initializing GemFire Modules Version 1.1>

    SLF4J can send this and other GemFire log messages to several logging frameworks. Refer to the SLF4J website for additional information.