To install the distributed cache in your application, add the following JAR files to your application's classpath:
${TERRACOTTA_HOME}/ehcache/ehcache-terracotta-<version>.jar
The Terracotta client libraries, where <version> is the current version of the Ehcache-Terracotta JAR. The Enterprise trial kit contains
ehcache-terracotta-ee-<version>.jar
in place of
ehcache-terracotta-<version>.jar
.
${TERRACOTTA_HOME}/ehcache/ehcache-core-<ehcache-version>.jar
The Ehcache core libraries, where <ehcache-version> is the current version of Ehcache (2.0.0 or higher).
${TERRACOTTA_HOME}/ehcache/slf4j-api-1.5.8.jar
The SLF4J logging facade allows Ehcache to bind to any supported logger used by your application. Binding JARs for popular logging options are available from the
SLF4J project
. For convenience, the binding JAR for
java.util.logging
is provided in
${TERRACOTTA_HOME}/ehcache
(see below).
${TERRACOTTA_HOME}/ehcache/slf4j-jdk14-1.5.8.jar
An SLF4J binding JAR for use with the standard
java.util.logging
, also known as JDK 1.4 logging.
If you are using a WAR file, add these JAR files to the
WEB-INF/lib
directory.
|
Most application servers (or web containers) should work with this installation of the Terracotta Distributed Cache. However, note the following:
- 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> |
Hibernate entities that should be cached must be marked in one of the following ways:
hbm.xml
file)
hibernate.cfg.xml
by default).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:
Transactional caches are supported with Echache 2.0 or later. See Setting Up Transactional Caches for more information on configuring a transactional cache.
See Cache Concurrency Strategies for more information on selecting a cache concurrency strategy.
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.
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.
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.
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.
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>
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.
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
.
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>
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.
You must start both your application and a Terracotta server.
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.
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>
tc-config.xml
.
tc-config.xml
to a location accessible to the Terracotta servers.Be sure to install your application and any application servers on each node.
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.
ehcache.xml
to each application node and ensure that it is on your application's classpath (or in
WEB-INF/classes
for web applications).
tc-config.xml
:[PROMPT] ${TERRACOTTA_HOME}/bin/start-tc-server.sh -f <path/to/tc-config.xml> -n Server1 &
[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.
To learn more about working with a Terracotta cluster, see the following documents:
tc-config.xml
is propagated and loaded in a Terracotta cluster in different environments.Top of 2.1 Terracotta Distributed Ehcache for Hibernate Express Installation