Buscar este blog

domingo, 24 de julio de 2016

Jboss - Hibernate - Infinispan cache

This is the basic configuration in order to use Hibernate Second Level Cache (2LC) inside a JBoss Server in a cluster enviroment. The cache provider will be Infinispan, wich is shipped with the server.
Second Level Cache (2LC)
A Second Level Cache (2LC) is a data store which holds persistent information relating to application state. Examples of 2LC consumers are session state replication, Single Sign On (SSO), and Java Persistence API (JPA). JBoss Enterprise Application Platform 6 uses Infinispan to manage its 2LC.
There is also a First Level Cache which is always enabled, but it works at the session level. The Hiberante Session, usually, is created when a transaction is opened and it is bounded to it. If during the life span of the transaction you get the same object two times from database, only in the first one Hibernate hits the database, the second time, the object is recovered from session.
The limitation with First Level Cache is that it only apply during the transaction, and only for the client who started it.

JBoss configuration

You need to use, at least, the HA profile (High Availability) .

Inside infinispan subsystem there should be an hibernate cache container, but it must be configured with the start attribute to EAGER.
/profile=ha/subsystem=infinispan/cache-container=hibernate:write-attribute(name=start, value=EAGER)

The result would be something like this:
<cache-container name="hibernate" start="EAGER" default-cache="local-query" module="org.jboss.as.jpa.hibernate:4">
    <transport lock-timeout="60000"/>
    <local-cache name="local-query">
        <transaction mode="NONE"/>
        <eviction strategy="LRU" max-entries="10000"/>
        <expiration max-idle="100000"/>
    </local-cache>
    <invalidation-cache name="entity" mode="SYNC">
        <transaction mode="NON_XA"/>
        <eviction strategy="LRU" max-entries="10000"/>
        <expiration max-idle="100000"/>
    </invalidation-cache>
    <replicated-cache name="timestamps" mode="ASYNC">
        <transaction mode="NONE"/>
        <eviction strategy="NONE"/>
    </replicated-cache>
</cache-container>

As you can see, there are three declared caches: local-query, entity and timestamps, each one with its own type attending to the clusterization mode. Infinispan allows the following clustering modes:
  • Local mode. Better performance, but no cluster aware.
  • Replicated mode. All the nodes in the cluster have their own local cache copy. When one node modify its local cache, it sends a message (sychronous or asynchronous) to the other nodes so they can update their local copy too.
  • Invalidation mode. No data is shared. When a node modifies some data in its own local cache copy, it sends a message to the other nodes so they can clear this object from their local copy.
  • Replication mode. Each cached object exists only in one (or in a small number) of the nodes in the cluster. Instead of replicate an object in all the nodes, they are informed about who owns it.

When the number of nodes increase (bigger than 10) the best option is the replication mode.

Application configuration

Hibernate config

Configure hibernate session factory. With the hibernateProperties you can enable 2LC and force hibernate to use the JBoss cache container obtained via JNDI.
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory" lazy-init="false">
 (...)
 <property name="hibernateProperties">
 <props>
   (...)
   
     <!-- Cache -->
     <prop key="hibernate.cache.use_second_level_cache">true</prop>
     <prop key="hibernate.cache.use_query_cache">true</prop>            
     <prop key="hibernate.cache.infinispan.statistics">true</prop>   
     <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.infinispan.JndiInfinispanRegionFactory</prop>
     <prop key="hibernate.cache.infinispan.cachemanager">java:jboss/infinispan/container/hibernate</prop> 
                               
 </props>
 </property>  
</bean>

Hibernate will use two of them, plus two other news cache wich will be configured the application startup. As you can see in org.hibernate.cache.infinispan.InfinispanRegionFactory, these caches are:
  • entity
  • collection
  • timestamps
  • query
The default values of thesea caches are well documented in this post: Using Infinispan as JPA/Hibernate Second Level Cache Provider

Hibernate will not share entities, collections or querys among the nodes of the cluster in order to reduce the traffic. For example, each node will have an entity instaced cached in its local store, and when this instance is changed by one of them, the others will be informed to evicts it.

Dependency config

You need to import the following dependencys:
<dependencyManagement>
 <dependencies>
  <dependency>
   <groupId>org.jboss.bom.eap</groupId>
   <artifactId>jboss-javaee-6.0-with-hibernate</artifactId>
   <version>6.2.0.GA</version>
   <type>pom</type>
   <scope>import</scope>
  </dependency>
 </dependencies>
</dependencyManagement>  


<dependencies>
 (...)
 
 <dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <scope>provided</scope>
 </dependency>

 <dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-infinispan</artifactId>
  <scope>provided</scope>
 </dependency>
</dependencies>

Note that I'm declaring hibernate dependencies as provided, because I will use the jars shipped inside JBoss, so the WAR does not need to have them in its lib directory.


You have to declare thease dependencies in the META-INF/jboss-deployment-structure.xml file, so JBoss will add them to the classpath during the application boot:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
 <deployment>
  <dependencies>
   <module name="org.hibernate" />
   <module name="org.javassist" />
   <module name="org.infinispan" services="import"/>   
   <module name="org.jboss.ironjacamar.jdbcadapters"/>
  </dependencies>

 </deployment>
</jboss-deployment-structure>

Documentation

Available doc:
This doc belongs to wildfly, but it can also be applied to EAP 6:

No hay comentarios:

Publicar un comentario