Chapter 5. Architecture

We've now gone through all the main concepts and the configuration details; now we'll look a bit under the covers to understand a bit more about the architectural design of the Hibernate/JBoss Cache integration. Readers can skip this chapter if they aren't interested in a look under the covers.

5.1. Hibernate Interface to the Caching Subsystem

The rest of Hibernate interacts with the Second Level Cache subsystem via the org.hibernate.cache.RegionFactory interface. What implementation of the interface is used is determined by the value of the hibernate.cache.region.factory_class configuration property. The interface itself is straightforward:

void start(Settings settings, Properties properties) 
       throws CacheException;

void stop();

boolean isMinimalPutsEnabledByDefault();

long nextTimestamp();

EntityRegion buildEntityRegion(String regionName, 
                               Properties properties, 
                               CacheDataDescription metadata) 
            throws CacheException;

CollectionRegion buildCollectionRegion(String regionName, 
                                       Properties properties, 
                                       CacheDataDescription cdd) 
            throws CacheException;

QueryResultsRegion buildQueryResultsRegion(String regionName, 
                                           Properties properties) 
            throws CacheException;

TimestampsRegion buildTimestampsRegion(String regionName, 
                                       Properties properties) 
            throws CacheException;
  • The start method is invoked during SessionFactory startup and allows the region factory implementation to access all the configuration settings and initialize itself. The stop method is invoked during SessionFactory shutdown.

  • The various build***Region methods are invoked as Hibernate detects it needs to cache different data. Hibernate can invoke these methods multiple times, with different regionName values; each call results in the establishment of a separate area in the underlying JBoss Cache instance(s). For example, if an application includes an entity class org.example.Order and another entity class org.example.LineItem, you would see two calls to buildEntityRegion, one for the Order class and one for the LineItem class. (Note that it is possible, and recommended, to configure one or more shared regions for entities, collections and queries. See Section 4.2, “Organization of Data in the Cache” for some examples.)

  • For each call to a build***Region method, the region factory returns a region object implementing the EntityRegion, CollectionRegion, QueryResultsRegion or TimestampsRegion interface. Each interface specifies the needed semantics for caching the relevant type of data. Thereafter, the Hibernate core invokes on that region object to manage reading and writing data in the cache.

Next, we'll look at the architecture of how the JBoss Cache integration implements these interfaces, first in the case where a single JBoss Cache instance is used, next in the case where multiple instances are desired.