Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
  3. monitoring="autodetect" dynamicConfig="true">
  4.  
  5. <defaultCache
  6. maxEntriesLocalHeap="10000"
  7. eternal="false"
  8. timeToIdleSeconds="120"
  9. timeToLiveSeconds="120"
  10. diskSpoolBufferSizeMB="30"
  11. maxEntriesLocalDisk="10000000"
  12. memoryStoreEvictionPolicy="LRU"
  13. statistics="true">
  14. <persistence strategy="localTempSwap" />
  15. </defaultCache>
  16.  
  17. <cache name="com.slc.entities.Movie"
  18. maxEntriesLocalHeap="100"
  19. maxEntriesLocalDisk="100"
  20. eternal="false"
  21. diskSpoolBufferSizeMB="20"
  22. timeToIdleSeconds="300"
  23. timeToLiveSeconds="600"
  24. memoryStoreEvictionPolicy="LFU"
  25. transactionalMode="off">
  26. <persistence strategy="localTempSwap" />
  27. </cache>
  28.  
  29. <cache
  30. name="org.hibernate.cache.StandardQueryCache"
  31. maxEntriesLocalHeap="5"
  32. eternal="false"
  33. timeToLiveSeconds="120">
  34. <persistence strategy="localTempSwap"/>
  35. </cache>
  36. </ehcache>
  37.  
  38.  
  39. hibernate.cfg.xml
  40.  
  41. <?xml version="1.0" encoding="UTF-8"?>
  42. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  43. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  44. <hibernate-configuration>
  45. <session-factory>
  46. <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
  47. <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  48. <property name="connection.username">usr</property>
  49. <property name="connection.password">pwd</property>
  50. <property name="hibernate.hbm2ddl.auto">update</property>
  51.  
  52. <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  53. <property name="show_sql">true</property>
  54.  
  55. <property name="hibernate.cache.use_second_level_cache">true</property>
  56.  
  57. <property name="hibernate.cache.use_query_cache">true</property>
  58. <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
  59.  
  60. <!-- Generate Statistics about the Second Level Cache. -->
  61. <property name="hibernate.generate_statistics">true</property>
  62.  
  63. <mapping resource="com/slc/entities/Movie.hbm.xml" />
  64.  
  65. <class-cache usage="read-write" class="com.slc.entities.Movie"/>
  66. </session-factory>
  67. </hibernate-configuration>
  68.  
  69. <?xml version="1.0"?>
  70. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  71. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  72. <hibernate-mapping package="com.slc.entities">
  73.  
  74. <class name="Movie" table="MOVIE" mutable="false">
  75. <cache usage="read-write" region="com.slc.entities.Movie" />
  76.  
  77. <id name="movieId" column="MOVIE_ID">
  78. <generator class="increment"/>
  79. </id>
  80. <property name="movieName" column="MOVIE_NAME"/>
  81. <property name="genere" column="GENERE"/>
  82. <property name="releaseYear" column="RELEASE_YEAR"/>
  83. </class>
  84. </hibernate-mapping>
  85.  
  86. public class SLCTest {
  87.  
  88. public static void main(String[] args) {
  89. Movie movie = null;
  90. try {
  91.  
  92. // Initialize Sessions
  93. SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
  94. Statistics stats = sessionFactory.getStatistics();
  95. System.out.println("Stats enabled=" + stats.isStatisticsEnabled());
  96.  
  97. Session session = sessionFactory.openSession();
  98. Session otherSession = sessionFactory.openSession();
  99. Transaction transaction = session.beginTransaction();
  100. Transaction otherTransaction = otherSession.beginTransaction();
  101.  
  102. printStats(stats, 0);
  103.  
  104. movie = (Movie) session.load(Movie.class, 1);
  105. printData(movie, stats, 1);
  106.  
  107. movie = (Movie) session.load(Movie.class, 1);;
  108. printData(movie, stats, 2);
  109.  
  110. // clear first level cache, so that second level cache is used
  111. session.evict(movie);
  112. movie = (Movie) session.load(Movie.class, 1);
  113. printData(movie, stats, 3);
  114.  
  115. movie = (Movie) session.load(Movie.class, 3);
  116. printData(movie, stats, 4);
  117. transaction.commit();
  118. session.close();
  119.  
  120. movie = (Movie) otherSession.load(Movie.class, 1);
  121. printData(movie, stats, 5);
  122.  
  123. // Release resources
  124.  
  125. otherTransaction.commit();
  126. sessionFactory.close();
  127.  
  128. } finally {
  129. HibernateUtil.closeSessionFactory();
  130. }
  131. }
  132.  
  133. private static void printStats(Statistics stats, int i) {
  134. System.out.println("***** " + i + " *****");
  135. System.out.println("Fetch Count=" + stats.getEntityFetchCount());
  136. System.out.println("Second Level Hit Count="
  137. + stats.getSecondLevelCacheHitCount());
  138. System.out.println("Second Level Miss Count="
  139. + stats.getSecondLevelCacheMissCount());
  140. System.out.println("Second Level Put Count="
  141. + stats.getSecondLevelCachePutCount());
  142. System.out.println("****** end stats ******");
  143. }
  144.  
  145. private static void printData(Movie movie, Statistics stats, int count) {
  146. System.out.println(movie);
  147. System.out.println(movie.getMovieName());
  148. printStats(stats, count);
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement