The Apache Ignite Integrations Developer Hub

Welcome to the Apache Ignite Integrations developer hub. You'll find comprehensive guides and documentation to help you start working with Apache Ignite Integrations as quickly as possible, as well as support if you get stuck. Let's jump right in!

Overview

Ignite In-Memory Data Fabric can be used as Hibernate Second-Level cache (or L2 cache), which can significantly speed-up the persistence layer of your application.

Hibernate is a well-known and widely used framework for Object-Relational Mapping (ORM). While interacting closely with an SQL database, it performs caching of retrieved data to minimize expensive database requests.

All work with Hibernate database-mapped objects is done within a session, usually bound to a worker thread or a Web session. By default, Hibernate only uses per-session (L1) cache, so, objects, cached in one session, are not seen in another. However, a global second-level (L2) cache may be used, in which the cached objects are seen for all sessions that use the same L2 cache configuration. This usually gives a significantly greater performance gain, because each newly-created session can take full advantage of the data already present in L2 cache memory (which outlives any session-local L1 cache).

While L1 cache is always enabled and fully implemented by Hibernate internally, L2 cache is optional and can have multiple pluggable implementaions. Ignite can be easily plugged-in as an L2 cache implementation, and can be used in all access modes (READ_ONLY, READ_WRITE, NONSTRICT_READ_WRITE, and TRANSACTIONAL), supporting a wide range of related features:

caching to memory and disk, as well as off-heap memory.

cache transactions, that make TRANSACTIONAL mode possible.

clustering, with 2 different replication modes: REPLICATED and PARTITIONED

To start using GridGain as a Hibernate L2 cache, you need to perform 3 simple steps:

L2 Cache Configuration

To configure Ignite In-Memory Data Fabric as a Hibernate L2 cache, without any changes required to the existing Hibernate code, you need to:

Add either ignite-hibernate_5.1 or ignite-hibernate_4.2 module as a dependency to your project depending on whether Hibernate 5 or Hibernate 4 is used. Alternatively, you can copy JAR files of the same name from {apache_ignite_relese}/libs/optional to {apache_ignite_relese}/libs folder if you start an Apache Ignite node from a command line.

Ignite Configuration Example

<!-- Basic configuration for atomic cache. --><beanid="atomic-cache"class="org.apache.ignite.configuration.CacheConfiguration"abstract="true"><propertyname="cacheMode"value="PARTITIONED"/><propertyname="atomicityMode"value="ATOMIC"/><propertyname="writeSynchronizationMode"value="FULL_SYNC"/></bean><!-- Basic configuration for transactional cache. --><beanid="transactional-cache"class="org.apache.ignite.configuration.CacheConfiguration"abstract="true"><propertyname="cacheMode"value="PARTITIONED"/><propertyname="atomicityMode"value="TRANSACTIONAL"/><propertyname="writeSynchronizationMode"value="FULL_SYNC"/></bean><beanid="ignite.cfg"class="org.apache.ignite.configuration.IgniteConfiguration"><!-- Specify the name of the caching grid (should correspond to the one in Hibernate configuration).--><propertyname="igniteInstanceName"value="hibernate-grid"/>
...
<!-- Specify cache configuration for each L2 cache region (which corresponds to a full class name or a full association name).--><propertyname="cacheConfiguration"><list><!--Configurations for entity caches.--><beanparent="transactional-cache"><propertyname="name"value="com.mycompany.MyEntity1"/></bean><beanparent="transactional-cache"><propertyname="name"value="com.mycompany.MyEntity2"/></bean><beanparent="transactional-cache"><propertyname="name"value="com.mycompany.MyEntity1.children"/></bean><!-- Configuration for update timestamps cache. --><beanparent="atomic-cache"><propertyname="name"value="org.hibernate.cache.spi.UpdateTimestampsCache"/></bean><!-- Configuration for query result cache. --><beanparent="atomic-cache"><propertyname="name"value="org.hibernate.cache.internal.StandardQueryCache"/></bean></list></property>
...
</bean>

Here, we specify the cache configuration for each L2 cache region:

We use PARTITIONED cache to split the data between caching nodes. Another possible strategy is to enable REPLICATED mode, thus replicating a full dataset between all caching nodes. See Cache Distribution Models for more information.

We specify the cache name that corresponds an L2 cache region name (either a full class name or a full association name).

We use TRANSACTIONAL atomicity mode to take advantage of cache transactions.

We enable FULL_SYNC to be always fully synchronized with backup nodes.

Additionally, we specify a cache for update timestamps, which may be ATOMIC, for better performance.

Having configured Ignite caching node, we can start it from within our code the following way:

The nodes may be started on other hosts as well, forming a distributed caching cluster. Be sure to specify the right network settings in GridGain configuration file for that.

Query Cache

In addition to L2 cache, Hibernate offers a query cache. This cache stores the results of queries (either HQL or Criteria) with a given set of parameters, so, when you repeat the query with the same parameter set, it hits the cache without going to the database.

Query cache may be useful if you have a number of queries, which may repeat with the same parameter values. Like in case of L2 cache, Hibernate relies on a 3-rd party cache implementation, and Ignite In-Memory Data Fabric can be used as such.

Consider using Apache Ignite SQL Grid directly which should perform faster than when it's called via Hibernate.

Query Cache Configuration

The configuration information above totally applies to query cache, but some additional configuration and code change is required.

Hibernate Configuration

To enable query cache in Hibernate, you only need one additional line in configuration file: