Hibernate.orgCommunity Documentation

Chapter 3. Configuration

3.1. Configuring the Hibernate Session Factory
3.1.1. Basics
3.1.2. Specifying the RegionFactory Implementation
3.1.3. The SharedJBossCacheRegionFactory
3.1.4. The JndiSharedJBossCacheRegionFactory
3.1.5. The MultiplexedJBossCacheRegionFactory
3.1.6. The JndiMultiplexedJBossCacheRegionFactory
3.1.7. Legacy Configuration Properties
3.2. Configuring JBoss Cache
3.2.1. Configuring a Single Standalone Cache
3.2.2. Managing Multiple Caches via a CacheManager
3.2.3. JBoss Cache Configuration Details
3.2.4. Standard JBoss Cache Configurations
3.3. JGroups Configuration
3.3.1. Transport -- UDP vs. TCP
3.3.2. Standard JGroups Configurations

There are three main areas of configuration involved in using JBoss Cache 3 for your Hibernate Second Level Cache: configuring the Hibernate SessionFactory, configuring the underlying JBoss Cache instance(s), and configuring the JGroups ChannelFactory. If you use the standard JBoss Cache and JGroups configurations that ship with the hibernate-jbosscache2.jar, then all you need to worry about is the SessionFactory configuration.

There are five basic steps to configuring the SessionFactory:

  • Make sure your Hibernate is configured to use JTA transactions. See Section 1.2.2, “JTA Transactional Support” for details.

  • Tell Hibernate you whether to enable caching of entities and collections. No need to set this property if you don't:

    hibernate.cache.use_second_level_cache=true
  • Tell Hibernate you want to enable caching of query results. No need to set this property if you don't:

    hibernate.cache.use_query_cache=true
  • If you have enabled caching of query results, tell Hibernate if you want to suppress costly replication of those results around the cluster. No need to set this property if you want query results replicated:

    hibernate.cache.jbc.query.localonly=true
  • Finally, you need to tell Hibernate what RegionFactory implementation to use to manage your caches. You do this by setting the hibernate.cache.region.factory_class configuration option.

    hibernate.cache.region.factory_class=
       org.hibernate.cache.jbc.MultiplexedJBossCacheRegionFactory

    To determine the correct factory class, you must decide whether you need just one underlying JBoss Cache instance to support the different types of caching you will be doing, or whether you need more than one. See Chapter 2, Core Concepts and particularly Section 2.3.4, “Bringing It All Together” for more on how to make that decision. Once you know the answer, see Section 3.1.2, “Specifying the RegionFactory Implementation” to find the factory class that best meets your needs.

    Once you've specified your factory class, there may be other factory-class-specific configurations you may want to set. The available options are explained below.

The MultiplexedJBossCacheRegionFactory supports a number of additional configuration options:

Many of the default values name JBoss Cache configurations in the standard jbc-configs.xml file found in the hibernate-jbosscache.jar. See Section 3.2.4, “Standard JBoss Cache Configurations” for details on those configurations. If you want to set hibernate.cache.jbc.configs and use your own JBoss Cache configuration file, you can still take advantage of these default names; just name the configurations in your file to match.

This is all looks a bit complex, so let's show what happens if you just configure the defaults, with query caching enabled:

hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=
   org.hibernate.cache.jbc.MultiplexedJBossCacheRegionFactory

You would end up using two JBoss Cache instances:

  • One, for the entities, collection and queries, would use the optimistic-entity configuration. This cache would use optimistic locking, synchronous invalidation and would disable initial state transfer.

  • The second, for timestamps, would use the timestamps-cache configuration. This cache would use pessimistic locking, asynchronous replication and would enable initial state transfer.

See Section 3.2.4, “Standard JBoss Cache Configurations” for more on these standard cache configurations.

If you hadn't set hibernate.cache.use_query_cache=true you'd just have the single optimistic-entity cache, shared by the entities and collections.

JBoss Cache provides a great many different configuration options; here we are just going to look at a few that are most relevant to the Second Level Cache use case. Please see the JBoss Cache User Guide for full details.

Let's look at how to specify a few of the JBoss Cache configuration options that are most relevant to the Hibernate use case.

The JBoss Cache CacheMode attribute encapsulates whether the cache uses replication, invalidation or local mode, as well as whether messages should be synchronous or asynchronous. See Section 2.2.1, “Replication vs. Invalidation vs. Local Mode” and Section 2.2.2, “Synchronous vs. Asynchronous” for a discussion of these concepts.

The CacheMode is configured as follows:

<!-- Legal modes are LOCAL
                     REPL_ASYNC
                     REPL_SYNC
                     INVALIDATION_ASYNC
                     INVALIDATION_SYNC
-->
<attribute name="CacheMode">INVALIDATION_SYNC</attribute>

The JBoss Cache NodeLockingScheme attribute configures whether optimistic locking or pessimistic locking should be used. See Section 2.2.3, “Locking Scheme” for a discussion of locking.

The NodeLockingScheme is configured as follows:

<!-- Node locking scheme:
        MVCC
        PESSIMISTIC (default)
-->
<attribute name="NodeLockingScheme">MVCC</attribute>

The JBoss Cache IsolationLevel attribute configures whether READ_COMMITTED or REPEATABLE_READ semantics are needed if pessimistic locking is used. It has no effect if optimistic locking is used. See Section 2.2.4, “Isolation Level” for a discussion of isolation levels.

The IsolationLevel is configured as follows:

<!-- Isolation level:
        READ_COMMITTED
        REPEATABLE_READ
-->
<attribute name="IsolationLevel">READ_COMMITTED</attribute>

See Section 2.2.5, “Initial State Transfer” for a discussion of the concept of initial state transfer.

Initial State Transfer is configured as follows:

<!-- Whether or not to fetch state on joining a cluster. -->
<attribute name="FetchInMemoryState">false</attribute>

Hibernate ships with a number of standard JBoss Cache configurations in the hibernate-jbosscache.jar's jbc-configs.xml file. The following table highlights the key features of each configuration.


A few notes on the above table:

These standard configurations are a good choice for many applications. The primary reason users may want to use their own configurations is to support more complex eviction setups. See Chapter 4, Cache Eviction for more on the kinds of things you can do with eviction.

JGroups configuration is a complex area that goes well beyond the scope of this document. Users interested in exploring the details are encouraged to visit the JGroups website at http://www.jgroups.org as well as the JGroups wiki page at jboss.com.

The jgroups-stacks.xml file found in the org.hibernate.cache.jbc.builder package in the hibernate-jbosscache.jar provides a good set of standard JGroups configurations; these should be suitable for most needs. If you need to create your own configuration set, we recommend that you start with this file as a base.

The JGroups transport refers to the mechanism JGroups uses for sending messages to the group members. Choosing which transport to use is the main JGroups-related decision users will need to make. There are three transport types: