Terracotta Logo (http://www.terracotta.org)

Terracotta Documentation Home

Terracotta 3.2.1 Documentation

Table of Contents •  Back  •  Forward


Register

2.1 Terracotta Distributed Ehcache for Hibernate Express Installation

2.1.1 Step 1: Requirements

2.1.2 Step 2: Install and Update the JAR files

To install the distributed cache in your application, add the following JAR files to your application's classpath:

If you are using a WAR file, add these JAR files to the WEB-INF/lib directory.

NOTE: Application Servers

Most application servers (or web containers) should work with this installation of the Terracotta Distributed Cache. However, note the following:
- GlassFish – You must add the following to domains.xml :
<jvm-options>-Dcom.sun.enterprise.server.ss.ASQuickStartup=false</jvm-options>
- WebLogic – You must use the supported version of WebLogic. If using version 10.3, you must remove the xml-apis from WEB-INF/lib and add the following to WEB-INF/weblogic.xml :
<weblogic-web-app>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>
- JBoss 5.x – PermGen memory must be at least 128MB and can be set using the switch -XX:MaxPermSize=128m .

2.1.3 Step 3: Prepare Your Application for Caching

Hibernate entities that should be cached must be marked in one of the following ways:

For more information on configuring Hibernate, including configuring collections for caching, see the Hibernate documentation .

In addition, you must specify a concurrency strategy for each cached entity. The following cache concurrency strategies are supported:

See Cache Concurrency Strategies for more information on selecting a cache concurrency strategy.

2.1.3a Using @Cache

Add the @Cache annotation to all entities in your application code that should be cached:

@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Foo {...}

@Cache must set the cache concurrency strategy for the entity, which in the example above is READ_WRITE.

2.1.3b Using the <cache> Element

In the Hibernate mapping file ( hbm.xml file) for the target entity, set caching for the entity using the <cache> element:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="com.my.package">
  <class name="Foo" table="BAR">
    <cache usage="read-write"/>
    <id name="id" column="BAR_ID">
      <generator class="native"/>
    </id>
    <!-- Some properties go here. -->
  </class>
</hibernate-mapping>

Use the usage attribute to specify the concurrency strategy.

2.1.3c Using the <class-cache> Element

In hibernate.cfg.xml , set caching for an entity by using <class-cache>, a subelement of the <session-factory> element:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
  <session-factory name="java:some/name">
    <!-- Properties go here. -->
 
    <!-- mapping files -->
    <mapping resource="com/my/package/Foo.hbm.xml"/>
 
    <!-- cache settings -->
    <class-cache class="com.my.package.Foo" usage="read-write"/>
  </session-factory>
</hibernate-configuration>

Use the usage attribute to specify the concurrency strategy.

2.1.4 Step 4: Edit Configuration Files

You must edit the Hibernate configuration file to enable and specify the second-level cache provider. You must also edit the Terracotta Distributed Ehcache for Hibernate configuration file to configure caching for the Hibernate entities that will be cached and to enable Terracotta clustering.

2.1.4a Hibernate Configuration File

For Hibernate 3.3, you can improve performance by substituting a factory class for the provider class used in previous versions of Hibernate. Add the following to your hibernate.cfg.xml file:

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>

For Hibernate 3.2, which cannot use the factory class, add the following to your hibernate.cfg.xml file:

<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property>

 

TIP: Singletons

To use a singleton version of the provider or factory class, substitute net.sf.ehcache.hibernate.SingletonEhCacheProvider or net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory .
Singleton CacheManagers are simpler to access and use, and can be helpful in less complex setups where only one configuration is required. Note that a singleton CacheManager should not be used in setups requiring mutliple configuration resources or involving multiple instances of Hibernate.

TIP: Spring Users

If you are configuring Hibernate using a Spring context file, you can enable and set the second-level cache provider using values in the hibernateProperties property in the bean definition for the session factory:
  <bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    </property>
<!-- Other properties, such as "mappingResources" listing Hibernate mapping files. -->
    <property name="hibernateProperties">
      <value>
        hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=
                 net.sf.ehcache.hibernate.EhCacheRegionFactory
<!-- Other values, such as hibernate.dialect. -->
      </value>
    </property>
  </bean>

2.1.4b Distributed Ehcache Configuration File

Create a basic Ehcache configuration file, ehcache.xml by default:

<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="Foo" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="ehcache.xsd">
      <defaultCache
        maxElementsInMemory="0"
        eternal="false"
        timeToIdleSeconds="1200"
        timeToLiveSeconds="1200">
          <terracotta />
      </defaultCache>
      <terracottaConfig url="localhost:9510" />
</ehcache>

This defaultCache configuration includes Terracotta clustering. The Terracotta client must load the configuration from a file or a Terracotta server. The value of the <terracottaConfig /> element’s url attribute should contain a path to the file or the address and DSO port (9510 by default) of a server. In the example value, "localhost:9510" means that the Terracotta server is on the local host.

TIP: Terracotta Clients and Servers

In a Terracotta cluster, the application server is also known as the client.

ehcache.xml must be on your application's classpath. If you are using a WAR file, add the Ehcache configuration file to WEB-INF/classes or to a JAR file that is included in WEB-INF/lib .

Specifying Caches for Hibernate Entities

Using an Ehcache configuration file with only a defaultCache configuration means that every cached Hibernate entity is cached with the settings of that defaultCache. You can create specific cache configurations for Hibernate entities using <cache> elements.

For example, add the following <cache> block to ehcache.xml to cache a Hibernate entity that has been configured for caching (see Step 3: Prepare Your Application for Caching):

<cache name="com.my.package.Foo" maxElementsInMemory="1000" 
       maxElementsOnDisk="10000" eternal="false" timeToIdleSeconds="3600" 
       timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU"> 
  <!-- Adding the element <terracotta /> turns on Terracotta clustering for the cache Foo. -->
  <terracotta /> 
</cache> 
Eviction Settings

You can edit the eviction settings in the defaultCache and any other caches that you configure in ehcache.xml to better fit your application’s requirements. See Eviction Parameters for more information.

2.1.5 Step 5: Start Your Application with the Cache

You must start both your application and a Terracotta server.

UNIX/Linux
 [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.sh &
Microsoft Windows
 [PROMPT] ${TERRACOTTA_HOME}\bin\start-tc-server.bat
  • 2. Start Your Application
  • Your application should now be running with the Terracotta second-level cache.

  • 3. Start the Terracotta Developer Console.
  • To view the cluster along with the cache, run the following command to start the Terracotta Developer Console:

UNIX/Linux
[PROMPT] ${TERRACOTTA_HOME}/bin/dev-console.sh &
Microsoft Windows
[PROMPT] ${TERRACOTTA_HOME}\bin\dev-console.bat
  • 4. On the console's initial panel, click Connect... .
  • 5. In the cluster navigation tree, navigate to Terracotta cluster > My application > Hibernate .
  • Hibernate and second-level cache statistics, as well as other visibility and control panels should be available.

2.1.6 Step 6: Edit the Terracotta Configuration

This step shows you how to run clients and servers on separate machines and add failover (High Availability). You will expand the Terracotta cluster and add High Availability by doing the following:

These tasks bring your cluster closer to a production architecture.

2.1.6a Procedure:

  • 1. Shut down the Terracotta cluster.
  • 2. Create a Terracotta configuration file called tc-config.xml with contents similar to the following:
<?xml version="1.0" encoding="UTF-8"?>
<!-- All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. -->
<tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd" xmlns:tc="http://www.terracotta.org/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  
  <servers>
    <!-- Sets where the Terracotta server can be found. Replace the value of 
         host with the server's IP address. -->
    <server host="server.1.ip.address" name="Server1">
      <data>%(user.home)/terracotta/server-data</data>
      <logs>%(user.home)/terracotta/server-logs</logs>
    </server>
    <!-- If using a standby Terracotta server, also referred to as an ACTIVE-
         PASSIVE configuration, add the second server here. -->
    <server host="server.2.ip.address" name="Server2">
      <data>%(user.home)/terracotta/server-data</data>
      <logs>%(user.home)/terracotta/server-logs</logs>
    </server>
    <ha>
       <mode>networked-active-passive</mode>
       <networked-active-passive>
          <election-time>5</election-time>
       </networked-active-passive>
    </ha>
  </servers>
  <!-- Sets where the generated client logs are saved on clients. -->  
  <clients>
    <logs>%(user.home)/terracotta/client-logs</logs>
  </clients>
</tc:tc-config>
  • 3. Install Terracotta 3.2.1 on a separate machine for each server you configure in tc-config.xml .
  • 4. Copy the tc-config.xml to a location accessible to the Terracotta servers.
  • 5. Perform Step 2: Install and Update the JAR files and Step 4: Edit Configuration Files steps on each application node you want to run in the cluster.
  • Be sure to install your application and any application servers on each node.

  • 6. Edit the <terracottaConfig> element in Terracotta Distributed Ehcache for Hibernate configuration file, ehcache.xml , that you created above:
<!-- Add the servers that are configured in tc-config.xml. -->
<terracottaConfig url="server.1.ip.address:9510,server.2.ip.address:9510" />

    Later in this procedure, you will see where to get more information on editing the settings in the configuration file.

  • 7. Copy ehcache.xml to each application node and ensure that it is on your application's classpath (or in WEB-INF/classes for web applications).
  • 8. Start the Terracotta server in the following way, replacing "Server1" with the name you gave your server in tc-config.xml :
UNIX/Linux
 [PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.sh -f <path/to/tc-config.xml> -n Server1 &
Microsoft Windows
 [PROMPT] ${TERRACOTTA_HOME}\bin\start-tc-server.bat -f <path\to\tc-config.xml> -n Server1 &

    If you configured a second server, start that server in the same way on its machine, entering its name after the -n flag. The second server to start up becomes the "hot" standby, or PASSIVE. Any other servers you configured will also start up as standby servers.

  • 9. Start all application servers.
  • 10. Start the Terracotta Developer Console and view the cluster.

2.1.7 Step 7: Learn More

To learn more about working with a Terracotta cluster, see the following documents:


Top of 2.1 Terracotta Distributed Ehcache for Hibernate Express Installation