Hi djeeva,
in my environment caching scalar query results in 2L-Cache works great (testcase below passes)
Maybe you can compare my result with yours and find out where the problem stays:
My Testcase:
Code:
/**
* N.B.: For this test-case hibernate.cache.use_second_level_cache can also be set to false
* @throws PUserException
*/
public void test2LScalarQueryCache() throws PUserException {
BartController ctrl = getBartController(getContext());
CompoundPersonInCharge root = ctrl.getNewCompoundPersonInCharge();
root.setFunktion("My-Root");
ctrl.createCompoundPersonInCharge(root);
addToDelete(root);
getContext().commit();
TransactionContext tcnt = (TransactionContext) getContext();
Session sess = (Session) tcnt._getSession().getEntityManager().getDelegate();
assertTrue("2L-Query-Caching (hibernate.cache.use_query_cache) must be enabled (check configuration in persistence.xml)!",
((SessionFactoryImpl)sess.getSessionFactory()).getSettings().isQueryCacheEnabled());
sess.getSessionFactory().getStatistics().clear();
PersonInCharge pers = ctrl.lookupPersonInCharge("My-Root");
assertNotNull(pers);
Date created = ((AbstractBasePersistentObject) pers).getCreateDate();
Session session = (Session) ((TransactionContext) getContext())._getSession().getEntityManager().getDelegate();
SQLQuery query = session.createSQLQuery("select count(*) as n1 from PersonInCharge where createDate = " + created.getDateAsLong());
IntegerType intt = new IntegerType();
query.addScalar("n1", intt);
query.setCacheable(true);
query.list();
assertEquals(0, sess.getSessionFactory().getStatistics().getQueryCacheHitCount());
assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCachePutCount());
getContext().commit();
session = (Session) ((TransactionContext) getContext())._getSession().getEntityManager().getDelegate();
query = session.createSQLQuery("select count(*) as n1 from PersonInCharge where createDate = " + created.getDateAsLong());
query.addScalar("n1", intt);
query.setCacheable(true);
int z = (int) query.uniqueResult();
assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCacheHitCount());
assertEquals(1, sess.getSessionFactory().getStatistics().getQueryCachePutCount());
assertEquals(1, z);
}
My Logging settings:
log4j.logger.org.hibernate=WARN
log4j.logger.org.hibernate.cache.internal.StandardQueryCache=TRACE
My 2nd-level-cache settings:
Code:
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
My OUTPUT :
Code:
...
14:05:52,271 DEBUG StandardQueryCache:130 - Checking cached query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache
14:05:52,271 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,272 TRACE StandardQueryCache:215 - querySpaces=[]
14:05:52,272 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,272 DEBUG StandardQueryCache:136 - Query results were not found in cache
p6spy - 1360587952277|4|9|statement
select
count(*) as n1
from
PersonInCharge
where
createDate = 20130211140552
p6spy - 1360587952277|-1||resultset|select count(*)=1
14:05:52,278 DEBUG StandardQueryCache:103 - Caching query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache; timestamp=5572968252526592
14:05:52,278 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,278 TRACE StandardQueryCache:215 - querySpaces=null
14:05:52,278 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,279 TRACE StandardQueryCache:265 - tuple is Object[1]; returnTypes is Type[1]
p6spy - 1360587952281|1|9|commit||
p6spy - 1360587952283|1|9|statement
BEGIN TRAN 'U00001#4_test2LScalarQueryCache' WITH MARK;select @@SPID
p6spy - 1360587952283|-1||resultset|=123
14:05:52,283 DEBUG StandardQueryCache:130 - Checking cached query results in region: hibernate.test.org.hibernate.cache.internal.StandardQueryCache
14:05:52,283 TRACE StandardQueryCache:214 - key.hashCode=783129251
14:05:52,283 TRACE StandardQueryCache:215 - querySpaces=[]
14:05:52,284 TRACE StandardQueryCache:231 - unexpected returnTypes is typename=integer class=java.lang.Integer ! result
14:05:52,284 DEBUG StandardQueryCache:188 - Checking query spaces are up-to-date: []
14:05:52,284 DEBUG StandardQueryCache:146 -[b] Returning cached query results[/b]
14:05:52,284 TRACE StandardQueryCache:265 - tuple is Object[1]; returnTypes is Type[1]
p6spy - 1360587952284|0|9|rollback||
p6spy - 1360587952286|0|9|statement
...
Environment: Hibernate Core 4.1.8, EHCACHE_CORE 2.5.1, SQLServer2008 R2