|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * Copyright (c) 2015, Red Hat Inc. or third-party contributors as |
| 5 | + * indicated by the @author tags or express copyright attribution |
| 6 | + * statements applied by the authors. All third-party contributions are |
| 7 | + * distributed under license by Red Hat Inc. |
| 8 | + * |
| 9 | + * This copyrighted material is made available to anyone wishing to use, modify, |
| 10 | + * copy, or redistribute it subject to the terms and conditions of the GNU |
| 11 | + * Lesser General Public License, as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this distribution; if not, write to: |
| 20 | + * Free Software Foundation, Inc. |
| 21 | + * 51 Franklin Street, Fifth Floor |
| 22 | + * Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | +package org.hibernate.jpa.test.query; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import javax.persistence.EntityManager; |
| 29 | +import javax.persistence.SharedCacheMode; |
| 30 | +import javax.persistence.TypedQuery; |
| 31 | + |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import org.hibernate.cfg.Environment; |
| 35 | +import org.hibernate.jpa.AvailableSettings; |
| 36 | +import org.hibernate.jpa.HibernateEntityManagerFactory; |
| 37 | +import org.hibernate.jpa.spi.HibernateEntityManagerImplementor; |
| 38 | +import org.hibernate.jpa.QueryHints; |
| 39 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 40 | +import org.hibernate.stat.Statistics; |
| 41 | +import org.hibernate.testing.TestForIssue; |
| 42 | + |
| 43 | +import static org.junit.Assert.assertEquals; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Gail Badner |
| 47 | + */ |
| 48 | +@TestForIssue( jiraKey = "HHH-9573" ) |
| 49 | +public class CachedQueryTest extends BaseEntityManagerFunctionalTestCase { |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testCacheableQuery() { |
| 53 | + EntityManager em = getOrCreateEntityManager(); |
| 54 | + em.getTransaction().begin(); |
| 55 | + for ( int i = 0 ; i < 10 ; i++ ) { |
| 56 | + Employee employee = new Employee( "John" + i, 20d + i); |
| 57 | + em.persist( employee ); |
| 58 | + } |
| 59 | + em.getTransaction().commit(); |
| 60 | + em.close(); |
| 61 | + |
| 62 | + HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) entityManagerFactory(); |
| 63 | + Statistics stats = hemf.getSessionFactory().getStatistics(); |
| 64 | + |
| 65 | + assertEquals( 0, stats.getQueryCacheHitCount() ); |
| 66 | + assertEquals( 0, stats.getQueryCacheMissCount() ); |
| 67 | + assertEquals( 0, stats.getQueryCachePutCount() ); |
| 68 | + assertEquals( 0, stats.getSecondLevelCacheHitCount() ); |
| 69 | + assertEquals( 0, stats.getSecondLevelCacheMissCount() ); |
| 70 | + assertEquals( 10, stats.getSecondLevelCachePutCount() ); |
| 71 | + |
| 72 | + stats.clear(); |
| 73 | + |
| 74 | + em = getOrCreateEntityManager(); |
| 75 | + em.getTransaction().begin(); |
| 76 | + |
| 77 | + // First time the query is executed, query and results are cached. |
| 78 | + |
| 79 | + TypedQuery<Employee> query = em.createQuery( "select e from Employee e", Employee.class ) |
| 80 | + .setHint( QueryHints.HINT_CACHEABLE, true ); |
| 81 | + List<Employee> employees = query.getResultList(); |
| 82 | + assertEquals( 10, employees.size() ); |
| 83 | + assertEquals( 0, stats.getQueryCacheHitCount() ); |
| 84 | + assertEquals( 1, stats.getQueryCacheMissCount() ); |
| 85 | + assertEquals( 1, stats.getQueryCachePutCount() ); |
| 86 | + // the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the |
| 87 | + // entities are read from the query ResultSet (not from the entity cache). |
| 88 | + assertEquals( 0, stats.getSecondLevelCacheHitCount() ); |
| 89 | + assertEquals( 0, stats.getSecondLevelCacheMissCount() ); |
| 90 | + assertEquals( 0, stats.getSecondLevelCachePutCount() ); |
| 91 | + |
| 92 | + em.getTransaction().commit(); |
| 93 | + em.close(); |
| 94 | + |
| 95 | + stats.clear(); |
| 96 | + |
| 97 | + // Second time the query is executed, list of entities are read from query cache and |
| 98 | + // the entities themselves are read from the entity cache. |
| 99 | + |
| 100 | + em = getOrCreateEntityManager(); |
| 101 | + em.getTransaction().begin(); |
| 102 | + query = em.createQuery( "select e from Employee e", Employee.class ) |
| 103 | + .setHint( QueryHints.HINT_CACHEABLE, true ); |
| 104 | + employees = query.getResultList(); |
| 105 | + assertEquals( 10, employees.size() ); |
| 106 | + assertEquals( 1, stats.getQueryCacheHitCount() ); |
| 107 | + assertEquals( 0, stats.getQueryCacheMissCount() ); |
| 108 | + assertEquals( 0, stats.getQueryCachePutCount() ); |
| 109 | + // the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the |
| 110 | + // entities are read from the query ResultSet (not from the entity cache). |
| 111 | + assertEquals( 10, stats.getSecondLevelCacheHitCount() ); |
| 112 | + assertEquals( 0, stats.getSecondLevelCacheMissCount() ); |
| 113 | + assertEquals( 0, stats.getSecondLevelCachePutCount() ); |
| 114 | + |
| 115 | + em.getTransaction().commit(); |
| 116 | + em.close(); |
| 117 | + |
| 118 | + // NOTE: JPACache.evictAll() only evicts entity regions; |
| 119 | + // it does not evict the collection regions or query cache region |
| 120 | + entityManagerFactory().getCache().evictAll(); |
| 121 | + |
| 122 | + stats.clear(); |
| 123 | + |
| 124 | + em = getOrCreateEntityManager(); |
| 125 | + em.getTransaction().begin(); |
| 126 | + query = em.createQuery( "select e from Employee e", Employee.class ) |
| 127 | + .setHint( QueryHints.HINT_CACHEABLE, true ); |
| 128 | + employees = query.getResultList(); |
| 129 | + assertEquals( 10, employees.size() ); |
| 130 | + // query is still found in the cache |
| 131 | + assertEquals( 1, stats.getQueryCacheHitCount() ); |
| 132 | + assertEquals( 0, stats.getQueryCacheMissCount() ); |
| 133 | + assertEquals( 0, stats.getQueryCachePutCount() ); |
| 134 | + // since entity regions were evicted, the 10 entities are not found, and are re-put after loading |
| 135 | + // as each entity ID is read from the query cache, Hibernate will look the entity up in the |
| 136 | + // cache and will not find it; that's why the "miss" and "put" counts are both 10. |
| 137 | + assertEquals( 0, stats.getSecondLevelCacheHitCount() ); |
| 138 | + assertEquals( 10, stats.getSecondLevelCacheMissCount() ); |
| 139 | + assertEquals( 10, stats.getSecondLevelCachePutCount() ); |
| 140 | + |
| 141 | + em.getTransaction().commit(); |
| 142 | + em.close(); |
| 143 | + |
| 144 | + stats.clear(); |
| 145 | + |
| 146 | + // this time call clear the entity regions and the query cache region |
| 147 | + em = getOrCreateEntityManager(); |
| 148 | + |
| 149 | + em.getEntityManagerFactory().getCache().evictAll(); |
| 150 | + em.unwrap( HibernateEntityManagerImplementor.class ) |
| 151 | + .getFactory() |
| 152 | + .getSessionFactory() |
| 153 | + .getCache() |
| 154 | + .evictQueryRegions(); |
| 155 | + |
| 156 | + em.getTransaction().begin(); |
| 157 | + query = em.createQuery( "select e from Employee e", Employee.class ) |
| 158 | + .setHint( QueryHints.HINT_CACHEABLE, true ); |
| 159 | + employees = query.getResultList(); |
| 160 | + assertEquals( 10, employees.size() ); |
| 161 | + // query is no longer found in the cache |
| 162 | + assertEquals( 0, stats.getQueryCacheHitCount() ); |
| 163 | + assertEquals( 1, stats.getQueryCacheMissCount() ); |
| 164 | + assertEquals( 1, stats.getQueryCachePutCount() ); |
| 165 | + // stats.getSecondLevelCacheHitCount() is 0 because the |
| 166 | + // entities are read from the query ResultSet (not from the entity cache). |
| 167 | + assertEquals( 0, stats.getSecondLevelCacheHitCount() ); |
| 168 | + assertEquals( 0, stats.getSecondLevelCacheMissCount() ); |
| 169 | + assertEquals( 10, stats.getSecondLevelCachePutCount() ); |
| 170 | + |
| 171 | + em.createQuery( "delete from Employee" ).executeUpdate(); |
| 172 | + em.getTransaction().commit(); |
| 173 | + em.close(); |
| 174 | + } |
| 175 | + |
| 176 | + protected void addConfigOptions(Map options) { |
| 177 | + options.put( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.ALL ); |
| 178 | + options.put( Environment.GENERATE_STATISTICS, "true" ); |
| 179 | + options.put( Environment.USE_QUERY_CACHE, "true" ); |
| 180 | + options.put( Environment.USE_SECOND_LEVEL_CACHE, "true" ); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Class[] getAnnotatedClasses() { |
| 185 | + return new Class[]{ |
| 186 | + Employee.class |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments