The following diagram illustrates the key elements involved when a single JBoss Cache instance is used:
For the single cache case, the user should specify
SharedJBossCacheRegionFactory
as their
hibernate.cache.region.factory_class
.
As part of its startup process, the region factory delegates
responsibility for providing JBoss Cache instances to an
implementation of the
org.hibernate.cache.jbc2.CacheInstanceManager
interface. The region factory separately requests a JBoss Cache
instance for entities, one for collections, one for queries and one
for timestamps. Whether the CacheInstanceManager
provides the same underlying JBoss Cache instance for each
request or provides multiple caches is an implementation detail
of the CacheInstanceManager
.
SharedJBossCacheRegionFactory
creates an
instance of SharedCacheInstanceManager
as
its CacheInstanceManager
.
SharedCacheInstanceManager
uses the JBoss Cache
configuration file specified by the user to create a single
org.jboss.cache.Cache
instance, and provides
that same instance to the region factory when it requests the
cache for entities, collections, queries and timestamps.
SharedCacheInstanceManager
also creates an
org.jgroups.ChannelFactory
and passes it to
the Cache
. The ChannelFactory
provides the cache with the org.jgroups.Channel
it uses for intra-cluster communication.
At this point, the region factory has a reference to a cache
for entities, a reference to a cache for collections, one for
queries and one for timestamps. In this particular case, each
reference points to the same underlying Cache
instance. When core Hibernate invokes the
buildEntityRegion
operation on the region
factory, it instantiates an implementation of the
EntityRegion
interface that knows how to
interface with JBoss Cache, passing it a reference to its
entity cache. Same thing happens for collections, etc.
Core Hibernate invokes on the EntityRegion
,
which in turn invokes read and write operations on the underlying
JBoss Cache. The cache uses its Channel
to
propagate changes to its peers in the cluster.
When the SessionFactory
shuts down, it
invokes stop()
on the region factory, which
in turn ensures that the JBoss Cache instance is stopped and
destroyed (which in turn closes the JGroups channel).