Clustering Made Easier Using Terracotta with Hibernate and/or Ehcache Cris J. Holdorph Software Architect Unicon, Inc. Jas...
Speaker Bio <ul><li>Michigan State University graduate
Java Developer since 1997
Unicon Employee since 1998 </li><ul><li>Cisco Networking Academy Program
uPortal
Sakai
Miscellaneous Professional Services </li></ul></ul><ul><li>http://www.unicon.net/blog/4 </li></ul>
Agenda <ul><li>Technologies
Using Ehcache
Using Ehcache with Terracotta
Using Hibernate
Using Hibernate with 2cd level cache
Terracotta Server
Other Considerations </li></ul>
Technologies
Technologies <ul><li>Ehcache
Hibernate
Terracotta
Spring Framework </li></ul>
Ehcache <ul><li>What is it? </li><ul><li>Generic Caching Platform </li></ul><li>www.ehcache.org </li><ul><li>Latest stable...
Used by both uPortal and Sakai </li></ul>
Hibernate <ul><li>What is it? </li><ul><li>Object Relational Mapping for Data Persistence
JPA implementation </li></ul><li>www.hibernate.org </li><ul><li>Latest stable release: 3.3.2 </li></ul><li>Acquired by JBo...
Used by both uPortal and Sakai </li></ul>
Terracotta <ul><li>What is it? </li><ul><li>Distributed Caching Platform
Clustered Caching Platform
Network Attached Memory </li></ul><li>www.terracotta.org </li><ul><li>Latest stable release: 3.2.0 </li></ul><li>Supported...
Open Source and Commercial
Unicon Partner
Many Terracotta employees are avid Twitter users </li></ul>
Spring Framework <ul><li>What is it? </li><ul><li>Dependency Injection Container
Multipurpose Java development libraries
And more... </li></ul><li>www.springframework.org </li><ul><li>Latest stable release: 3.0.0 </li></ul><li>Supported by Spr...
Strong integration with Ehcache, Hibernate
Some integration with Terracotta
Used by both uPortal and Sakai </li></ul>
Using Ehcache
Sample Application <ul><li>Census Collect Web Application </li><ul><li>List View (choose  State  to limit display)
Form View </li></ul><li>Census View Web Application </li></ul>
Sample Application (screenshots)
Sample Application (screenshots)
pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>1.7.2</version> </de...
ehcache.xml <ehcache> <diskStore path=&quot;java.io.tmpdir&quot; /> <defaultCache maxElementsInMemory=&quot;10&quot; etern...
CensusCollectServlet.java import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; ...
CensusCollectServlet.java ... Person person = null; Element element = peopleCache.get(ssn); if (person == null) { person =...
Using Ehcache with RMI Replication
ehcache.xml <cacheManagerPeerProviderFactory class=&quot;net.sf.ehcache.distribution. RMICacheManagerPeerProviderFactory&q...
Using Ehcache with Terracotta
pom.xml <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>1.7.2</version> </de...
Upcoming SlideShare
Loading in...5
×

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

2,175

Published on

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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,175
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

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

  1. 1. 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/
  2. 2. Speaker Bio <ul><li>Michigan State University graduate
  3. 3. Java Developer since 1997
  4. 4. Unicon Employee since 1998 </li><ul><li>Cisco Networking Academy Program
  5. 5. uPortal
  6. 6. Sakai
  7. 7. Miscellaneous Professional Services </li></ul></ul><ul><li>http://www.unicon.net/blog/4 </li></ul>
  8. 8. Agenda <ul><li>Technologies
  9. 9. Using Ehcache
  10. 10. Using Ehcache with Terracotta
  11. 11. Using Hibernate
  12. 12. Using Hibernate with 2cd level cache
  13. 13. Terracotta Server
  14. 14. Other Considerations </li></ul>
  15. 15. Technologies
  16. 16. Technologies <ul><li>Ehcache
  17. 17. Hibernate
  18. 18. Terracotta
  19. 19. Spring Framework </li></ul>
  20. 20. Ehcache <ul><li>What is it? </li><ul><li>Generic Caching Platform </li></ul><li>www.ehcache.org </li><ul><li>Latest stable release: 1.7.2 (2.0 in beta) </li></ul><li>Acquired by Terracotta in 2009 </li><ul><li>Greg Luck, Terracotta employee, Hibernate committer </li></ul><li>Gradually increased integration with Terracotta
  21. 21. Used by both uPortal and Sakai </li></ul>
  22. 22. Hibernate <ul><li>What is it? </li><ul><li>Object Relational Mapping for Data Persistence
  23. 23. JPA implementation </li></ul><li>www.hibernate.org </li><ul><li>Latest stable release: 3.3.2 </li></ul><li>Acquired by JBoss / RedHat
  24. 24. Used by both uPortal and Sakai </li></ul>
  25. 25. Terracotta <ul><li>What is it? </li><ul><li>Distributed Caching Platform
  26. 26. Clustered Caching Platform
  27. 27. Network Attached Memory </li></ul><li>www.terracotta.org </li><ul><li>Latest stable release: 3.2.0 </li></ul><li>Supported by Terracotta Tech
  28. 28. Open Source and Commercial
  29. 29. Unicon Partner
  30. 30. Many Terracotta employees are avid Twitter users </li></ul>
  31. 31. Spring Framework <ul><li>What is it? </li><ul><li>Dependency Injection Container
  32. 32. Multipurpose Java development libraries
  33. 33. And more... </li></ul><li>www.springframework.org </li><ul><li>Latest stable release: 3.0.0 </li></ul><li>Supported by Spring Source
  34. 34. Strong integration with Ehcache, Hibernate
  35. 35. Some integration with Terracotta
  36. 36. Used by both uPortal and Sakai </li></ul>
  37. 37. Using Ehcache
  38. 38. Sample Application <ul><li>Census Collect Web Application </li><ul><li>List View (choose State to limit display)
  39. 39. Form View </li></ul><li>Census View Web Application </li></ul>
  40. 40. Sample Application (screenshots)
  41. 41. Sample Application (screenshots)
  42. 42. 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>
  43. 43. 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>
  44. 44. 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;); } ...
  45. 45. 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)); ...
  46. 46. Using Ehcache with RMI Replication
  47. 47. 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>
  48. 48. Using Ehcache with Terracotta
  49. 49. 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>
  50. 50. 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>
  51. 51. 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.
  52. 52. Using Hibernate
  53. 53. 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> . . .
  54. 54. 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>
  55. 55. 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>
  56. 56. 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>
  57. 57. 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; }
  58. 58. Using Hibernate with a Second Level Cache Ehcache
  59. 59. 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> . . .
  60. 60. 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>
  61. 61. 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>
  62. 62. 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; }
  63. 63. Using Hibernate with a Second Level Cache Terracotta
  64. 64. 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> . . .
  65. 65. 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>
  66. 66. 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>
  67. 67. 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.
  68. 68. 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.
  69. 69. Terracotta Server (Array)
  70. 70. Terracotta Server <ul><li>Terracotta helps with Failover </li><ul><li>Don't make Terracotta a new single point of failure
  71. 71. Multiple Terracotta Servers in clustered mode
  72. 72. Terracotta Server Array </li></ul><li>tc-config.xml
  73. 73. Express mode vs Custom/Identity Mode
  74. 74. Administration Console </li></ul>$ /opt/terracotta-3.2.0/bin/ dev-console.sh
  75. 75. Other Considerations
  76. 76. Spring Framework <ul><li>Spring Framework makes working with _____ easier </li><ul><li>Ehcache
  77. 77. Hibernate
  78. 78. Terracotta </li></ul><li>Concepts remain, details change
  79. 79. Annotations anyone? </li></ul>
  80. 80. Downsides <ul><li>Yet Another ____ </li><ul><li>Technology for developers to learn
  81. 81. Service for system administrators to learn / support
  82. 82. Package to purchase support for? </li><ul><li>Free Terracotta Forums are really good
  83. 83. Twitter / Blogs are great
  84. 84. Employees are really accessible </li></ul><li>Point of failure </li><ul><li>Terracotta servers support fail-over </li></ul></ul></ul>
  85. 85. Gotchas <ul><li>Terracotta 3.1.1 requires Ehcache-terracotta 1.7.2
  86. 86. Terracotta 3.2.0 requires Ehcache-terracotta 1.8.0
  87. 87. Terracotta 3.2.1 (not yet released) will require Ehcache-terracotta 2.0.0
  88. 88. Ehcache-Terracotta Express mode fully serializes objects
  89. 89. Ehcache-Custom mode (also called Identity mode) requires custom tc-config.xml file </li></ul>
  90. 90. Gotchas (2) <ul><li>Don't forget the JAVA_OPTS if using Terracotta as a Hibernate second level cache </li><ul><li>http://tr.im/Qz61
  91. 91. This requires specifying a jar as a javagent, this jar can NOT be on the classpath </li></ul><li>Don't forget to start your database and Terracotta server before starting Tomcat </li></ul>
  92. 92. References <ul><li>Forums </li><ul><li>forums.terracotta.org
  93. 93. forums.hibernate.org </li></ul><li>Twitter </li><ul><li>Use the word Terracotta in your Twitter message and see which Terracotta Tech employees start following you </li></ul><li>Websites </li><ul><li>http://www.terracotta.org
  94. 94. http://www.ehcache.org
  95. 95. http://www.hibernate.org </li></ul></ul>
  96. 96. Questions & Answers Cris J. Holdorph Software Architect Unicon, Inc. [email_address] www.unicon.net

×