• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache
 

Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache

on

  • 2,575 views

As presented at the Jasig (www.jasig.org) conference in San Diego, March 2010.

As presented at the Jasig (www.jasig.org) conference in San Diego, March 2010.

Statistics

Views

Total Views
2,575
Views on SlideShare
2,568
Embed Views
7

Actions

Likes
3
Downloads
0
Comments
0

1 Embed 7

http://www.slideshare.net 7

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache Clustering Made Easier: Using Terracotta with Hibernate and/or EHCache Presentation Transcript

    • Clustering Made Easier Using Terracotta with Hibernate and/or Ehcache Cris J. Holdorph Software Architect Unicon, Inc. Jasig Conference San Diego, CA March 9, 2010 © Copyright Unicon, Inc., 2008. Some rights reserved. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/
    • Speaker Bio
      • Michigan State University graduate
      • Java Developer since 1997
      • Unicon Employee since 1998
        • Cisco Networking Academy Program
        • uPortal
        • Sakai
        • Miscellaneous Professional Services
      • http://www.unicon.net/blog/4
    • Agenda
      • Technologies
      • Using Ehcache
      • Using Ehcache with Terracotta
      • Using Hibernate
      • Using Hibernate with 2cd level cache
      • Terracotta Server
      • Other Considerations
    • Technologies
    • Technologies
      • Ehcache
      • Hibernate
      • Terracotta
      • Spring Framework
    • Ehcache
      • What is it?
        • Generic Caching Platform
      • www.ehcache.org
        • Latest stable release: 1.7.2 (2.0 in beta)
      • Acquired by Terracotta in 2009
        • Greg Luck, Terracotta employee, Hibernate committer
      • Gradually increased integration with Terracotta
      • Used by both uPortal and Sakai
    • Hibernate
      • What is it?
        • Object Relational Mapping for Data Persistence
        • JPA implementation
      • www.hibernate.org
        • Latest stable release: 3.3.2
      • Acquired by JBoss / RedHat
      • Used by both uPortal and Sakai
    • Terracotta
      • What is it?
        • Distributed Caching Platform
        • Clustered Caching Platform
        • Network Attached Memory
      • www.terracotta.org
        • Latest stable release: 3.2.0
      • Supported by Terracotta Tech
      • Open Source and Commercial
      • Unicon Partner
      • Many Terracotta employees are avid Twitter users
    • Spring Framework
      • What is it?
        • Dependency Injection Container
        • Multipurpose Java development libraries
        • And more...
      • www.springframework.org
        • Latest stable release: 3.0.0
      • Supported by Spring Source
      • Strong integration with Ehcache, Hibernate
      • Some integration with Terracotta
      • Used by both uPortal and Sakai
    • Using Ehcache
    • Sample Application
      • Census Collect Web Application
        • List View (choose State to limit display)
        • Form View
      • Census View Web Application
    • Sample Application (screenshots)
    • Sample Application (screenshots)
    • pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version> </dependency>
    • ehcache.xml <ehcache> <diskStore path=&quot;java.io.tmpdir&quot; /> <defaultCache maxElementsInMemory=&quot;10&quot; eternal=&quot;false&quot; timeToIdleSeconds=&quot;120&quot; timeToLiveSeconds=&quot;300&quot; overflowToDisk=&quot;true&quot; /> <cache name=&quot;PEOPLE_CACHE&quot; maxElementsInMemory=&quot;2000&quot; eternal=&quot;false&quot; overflowToDisk=&quot;true&quot; timeToIdleSeconds=&quot;0&quot; timeToLiveSeconds=&quot;3600&quot; /> </ehcache>
    • CensusCollectServlet.java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; … private Cache peopleCache = null; … public void init(ServletConfig config) throws ServletException { super.init(config); CacheManager manager = new CacheManager(); peopleCache = manager.getCache(&quot;PEOPLE_CACHE&quot;); } ...
    • CensusCollectServlet.java ... Person person = null; Element element = peopleCache.get(ssn); if (person == null) { person = new Person(); } else { person = (Person)element.getValue(); } request.setAttribute(PERSON, person); ... peopleCache.put(new Element(ssn, person)); ...
    • Using Ehcache with RMI Replication
    • ehcache.xml <cacheManagerPeerProviderFactory class=&quot;net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactory&quot; properties=&quot;hostName=fully_qualified_hostname_or_ip, peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32&quot;/> <cache name=&quot; PEOPLE_CACHE &quot; ...> <cacheEventListenerFactory class=&quot;net.sf.ehcache.distribution.RMICacheReplicatorFactory” properties=&quot;replicateAsynchronously=true, replicatePuts=true, replicatePutsViaCopy=false, replicateUpdates=true, replicateUpdatesViaCopy=true, replicateRemovals=true asynchronousReplicationIntervalMillis=1000&quot;/> </cache>
    • Using Ehcache with Terracotta
    • pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.5.8</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-terracotta</artifactId> <version>1.8.0</version> </dependency>
    • ehcache.xml <ehcache> <diskStore path=&quot;java.io.tmpdir&quot; /> <defaultCache maxElementsInMemory=&quot;10&quot; eternal=&quot;false&quot; timeToIdleSeconds=&quot;120&quot; timeToLiveSeconds=&quot;300&quot; overflowToDisk=&quot;true&quot; /> <terracottaConfig url=&quot;localhost:9510&quot;/> <cache name=&quot;PEOPLE_CACHE&quot; maxElementsInMemory=&quot;2000&quot; eternal=&quot;false&quot; overflowToDisk=&quot;true&quot; timeToIdleSeconds=&quot;0&quot; timeToLiveSeconds=&quot;3600&quot; <terracotta /> </cache> </ehcache>
    • Terracotta Server $ terracotta-3.2.0/bin/start-tc-server.sh Start the Terracotta Server First The Terracotta server must be started before any applications that use Terracotta.
    • Using Hibernate
    • hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot; &quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;> <hibernate-configuration> <session-factory> <property name=&quot;connection.driver_class&quot;>org.hsqldb.jdbcDriver</property> <property name=&quot;connection.url&quot;>jdbc:hsqldb:hsql://localhost:8887</property> <property name=&quot;connection.username&quot;>sa</property> <property name=&quot;connection.password&quot;></property> <property name=&quot;connection.pool_size&quot;>2</property> <property name=&quot;dialect&quot;>org.hibernate.dialect.HSQLDialect</property> . . .
    • hibernate.cfg.xml (2) . . . <property name=&quot;current_session_context_class&quot;>org.hibernate.context.ManagedSessionContext</property> <property name=&quot;cache.provider_class&quot;> org.hibernate.cache.NoCacheProvider </property> <property name=&quot;show_sql&quot;>true</property> <property name=&quot;hbm2ddl.auto&quot;>validate</property> <mapping resource=&quot;org/jasig/census/Person.hbm.xml&quot;/> </session-factory> </hibernate-configuration>
    • Person.hbm.xml <?xml version=&quot;1.0&quot;?> <!DOCTYPE hibernate-mapping PUBLIC &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot; &quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;> <hibernate-mapping package=&quot;org.jasig.census&quot;> <class name=&quot;Person&quot; table=&quot;PERSON&quot;> <cache usage=&quot;read-write&quot; /> <id name=&quot;id&quot; column=&quot;PERSON_ID&quot;> <generator class=&quot;native&quot;/> </id> <property name=&quot;name&quot;/> <property name=&quot;city&quot;/> <property name=&quot;state&quot;/> <property name=&quot;ssn&quot;/> </class> </hibernate-mapping>
    • pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.7</version> </dependency>
    • getAllPeople() (.java code) public static List<?> getAllPeople() { SessionFactory sf = getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); List<?> result = session.createQuery(&quot;from Person&quot;).list(); session.getTransaction().commit(); session.close(); return result; }
    • Using Hibernate with a Second Level Cache Ehcache
    • pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> . . .
    • hibernate.cfg.xml . . . <property name=&quot;current_session_context_class&quot;>org.hibernate.context.ManagedSessionContext</property> <property name=&quot;use_second_level_cache&quot;>true</property> <property name=&quot;cache.provider_class&quot;>org.hibernate.cache.EhCacheProvider</property> <property name=&quot;show_sql&quot;>true</property> <property name=&quot;hbm2ddl.auto&quot;>validate</property> <mapping resource=&quot;org/jasig/census/Person.hbm.xml&quot;/> </session-factory> </hibernate-configuration>
    • ehcache.xml <ehcache> <diskStore path=&quot;java.io.tmpdir&quot; /> <defaultCache maxElementsInMemory=&quot;10&quot; overflowToDisk=&quot;true&quot; timeToIdleSeconds=&quot;120&quot; timeToLiveSeconds=&quot;300&quot; eternal=&quot;false&quot; /> <cache name=&quot;org.jasig.census.Person&quot; maxElementsInMemory=&quot;10000&quot; eternal=&quot;false&quot; timeToIdleSeconds=&quot;3000&quot; timeToLiveSeconds=&quot;6000&quot; overflowToDisk=&quot;false&quot; /> <cache name=&quot;query.findAllPeople&quot; maxElementsInMemory=&quot;50&quot; eternal=&quot;false&quot; timeToLiveSeconds=&quot;86400&quot; overflowToDisk=&quot;false&quot; /> </ehcache>
    • getAllPeople() (.java code) public static List<?> getAllPeople() { SessionFactory sf = getSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); List<?> result = session.createQuery(&quot;from Person&quot;). setCacheable(true).setCacheRegion(&quot;query.findAllPeople&quot;). list(); session.getTransaction().commit(); session.close(); return result; }
    • Using Hibernate with a Second Level Cache Terracotta
    • pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.2.GA</version> </dependency> <dependency> <groupId>org.terracotta.hibernate</groupId> <artifactId>terracotta-hibernate-cache</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.8.0.GA</version> </dependency> . . .
    • pom.xml (2) . . . <repositories> <repository> <id>terracotta-repository</id> <url>http://www.terracotta.org/download/reflector/maven2</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>
    • hibernate.cfg.xml . . . <property name=&quot;current_session_context_class&quot;>org.hibernate.context.ManagedSessionContext</property> <property name=&quot;use_second_level_cache&quot;>true</property> <property name=&quot;cache.provider_class&quot;> org.terracotta.hibernate.TerracottaHibernateCacheProvider </property> <property name=&quot;show_sql&quot;>true</property> <property name=&quot;hbm2ddl.auto&quot;>validate</property> <mapping resource=&quot;org/jasig/census/Person.hbm.xml&quot;/> </session-factory> </hibernate-configuration>
    • Terracotta Server $ /opt/terracotta-3.2.0/bin/start-tc-server.sh Start the Terracotta Server First The Terracotta server must be started before any applications that use Terracotta.
    • Hibernate Application $ export JAVA_OPTS=&quot;-javaagent:/opt/terracotta-3.2.0/hibernate/terracotta-hibernate-agent-1.1.0.jar&quot; $ /opt/apache-tomcat-6.0.24/bin/startup.sh Start Application with Options The application must be started with special JAVA_OPTS set for Terracotta.
    • Terracotta Server (Array)
    • Terracotta Server
      • Terracotta helps with Failover
        • Don't make Terracotta a new single point of failure
        • Multiple Terracotta Servers in clustered mode
        • Terracotta Server Array
      • tc-config.xml
      • Express mode vs Custom/Identity Mode
      • Administration Console
      $ /opt/terracotta-3.2.0/bin/ dev-console.sh
    • Other Considerations
    • Spring Framework
      • Spring Framework makes working with _____ easier
        • Ehcache
        • Hibernate
        • Terracotta
      • Concepts remain, details change
      • Annotations anyone?
    • Downsides
      • Yet Another ____
        • Technology for developers to learn
        • Service for system administrators to learn / support
        • Package to purchase support for?
          • Free Terracotta Forums are really good
          • Twitter / Blogs are great
          • Employees are really accessible
        • Point of failure
          • Terracotta servers support fail-over
    • Gotchas
      • Terracotta 3.1.1 requires Ehcache-terracotta 1.7.2
      • Terracotta 3.2.0 requires Ehcache-terracotta 1.8.0
      • Terracotta 3.2.1 (not yet released) will require Ehcache-terracotta 2.0.0
      • Ehcache-Terracotta Express mode fully serializes objects
      • Ehcache-Custom mode (also called Identity mode) requires custom tc-config.xml file
    • Gotchas (2)
      • Don't forget the JAVA_OPTS if using Terracotta as a Hibernate second level cache
        • http://tr.im/Qz61
        • This requires specifying a jar as a javagent, this jar can NOT be on the classpath
      • Don't forget to start your database and Terracotta server before starting Tomcat
    • References
      • Forums
        • forums.terracotta.org
        • forums.hibernate.org
      • Twitter
        • Use the word Terracotta in your Twitter message and see which Terracotta Tech employees start following you
      • Websites
        • http://www.terracotta.org
        • http://www.ehcache.org
        • http://www.hibernate.org
    • Questions & Answers Cris J. Holdorph Software Architect Unicon, Inc. [email_address] www.unicon.net