diff --git a/core/src/main/java/org/hibernate/cache/StandardQueryCache.java b/core/src/main/java/org/hibernate/cache/StandardQueryCache.java index 85b211ca6e..85425211ac 100644 --- a/core/src/main/java/org/hibernate/cache/StandardQueryCache.java +++ b/core/src/main/java/org/hibernate/cache/StandardQueryCache.java @@ -88,7 +88,7 @@ public class StandardQueryCache implements QueryCache { return false; } else { - Long ts = Long.valueOf( session.getTimestamp() ); + Long ts = new Long( session.getFactory().getSettings().getRegionFactory().nextTimestamp()); if ( log.isDebugEnabled() ) { log.debug( "caching query results in region: " + cacheRegion.getName() + "; timestamp=" + ts ); diff --git a/testsuite/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java b/testsuite/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java index 124411d04b..83fe9cb47d 100755 --- a/testsuite/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java +++ b/testsuite/src/test/java/org/hibernate/test/querycache/QueryCacheTest.java @@ -1,6 +1,7 @@ //$Id: QueryCacheTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $ package org.hibernate.test.querycache; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -41,6 +42,48 @@ public class QueryCacheTest extends FunctionalTestCase { public static Test suite() { return new FunctionalTestClassTestSuite( QueryCacheTest.class ); } + //https://jira.jboss.org/jira/browse/JBPAPP-4224 + public void testHitCacheInSameSession() { + getSessions().evictQueries(); + getSessions().getStatistics().clear(); + Session s = openSession(); + List list = new ArrayList(); + s.beginTransaction(); + for ( int i = 0; i < 3; i++ ) { + Item a = new Item(); + a.setName( "a" + i ); + a.setDescription( "a" + i ); + list.add( a ); + s.persist( a ); + } + s.getTransaction().commit(); + +// s.close(); +// s=openSession(); + + s.beginTransaction(); + String queryString = "from Item"; + // this query will hit the database and create the cache + s.createQuery( queryString ).setCacheable( true ).list(); + s.getTransaction().commit(); + + s.beginTransaction(); + //and this one SHOULD served by the cache + s.createQuery( queryString ).setCacheable( true ).list(); + s.getTransaction().commit(); + QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics( queryString ); + assertEquals( 1, qs.getCacheHitCount() ); + assertEquals( 1, qs.getCachePutCount() ); + s.close(); + s = openSession(); + s.beginTransaction(); + for(Object obj:list){ + s.delete( obj ); + } + s.getTransaction().commit(); + s.close(); + + } public void testQueryCacheInvalidation() throws Exception {