How do I integrate Coherence and Hibernate?

Hibernate and Coherence can be used together in several combinations. This document discusses the various options, including when each one is appropriate, along with usage instructions. These options include using Coherence as a Hibernate plug-in and using Hibernate as a Coherence plug-in via the CacheStore interface. Most applications that use Coherence and Hibernate use a mixture of these approaches. The Hibernate API features powerful management of entities and relationships, and the Coherence API delivers maximum performance and scalability.

Choosing an Integration Strategy

There are two methods for integrating Coherence and Hibernate: Coherence can be used as an L2 cache provider for Hibernate and Hibernate can be used as a CacheStore provider for Coherence. These techniques are described in detail in Using Hibernate as a CacheStore for Coherence and Using Coherence as the Hibernate L2 Cache.

Using Coherence as a Hibernate L2 cache provider enables multiple JVMs running the same Hibernate application to share an L2 cache. The use of Coherence caching in this case is controlled by Hibernate; thus a good understanding of Hibernate L2 caching is required to successfully use this provider. This may be a good fit for applications that:

Hibernate can also be used as a CacheStore implementation for Coherence. Applications that use this approach typically have the following characteristics:

The attached example illustrates both integration approaches. See the included readme.txt for further instructions.

Selecting a Caching Strategy

Generally, the Hibernate API is the optimal choice for accessing data held in a relational database where performance is not the dominant factor. For application state (or any type of data that fits naturally into the Map interface) use the Coherence API. For performance-sensitive operations, specifically those that may benefit from Coherence-specific features like write-behind caching or cache queries, use the Coherence API.

Coherence and Hibernate Configuration Files

The Coherence cache configuration file is referred to as coherence-cache-config.xml (the default name) and the Hibernate root configuration file is referred to as hibernate.cfg.xml (the default name).

Hibernate API

The Hibernate API provides flexible queries and relational management features including referential integrity, cascading deletes and child object fetching. While these features may be implemented using Coherence, this involves development effort which may not be worthwhile in cases where performance is not an issue.

Coherence NamedCache API

There are many Coherence features that require direct access to the Coherence NamedCache API, including:

Direct access to these features may be critical for achieving the highest levels of scalable performance.

Coherence CacheStore Integration

CacheStore modules are useful for transparently keeping cache and database synchronized. They are also more efficient than independently updating the cache and database as updates are routed through Coherence's partitioning facilities, minimizing locking.

CacheStore modules give very high performance for caching that can be expressed via a Map interface, that is a key-value pair. The NamedCacheinterface is a much simpler and by extension much lower-overhead API than the Hibernate query API. Additionally, in some cases (where complex queries can be mapped into a key-based pattern), very complex queries can be answered by a simple cache retrieval.

One final reason for using CacheStore is that it provides a means of coordinating all database (or other backend) access through a single API (NamedCache) and through a controlled set of JVMs (server machines). This is because the nodes which are responsible for managing cache partitions are the same machines responsible for synchronizing with the database server.


Attachments:
hibernate-sample-src.jar (application/octet-stream)