Well, drama started while verifying that entities exist in a single Region (for this case org.openmrs.Person.names) before evicting a single entity, all of them or clearing the whole cache.
Below is the testing logic.
//SessionFactory sf = (SessionFactory) applicationContext.getBean("sessionFactory");
PersonName name = Context.getPersonService().getPersonName(PERSON_NAME_ID_2);
//Load the person so that the names are also stored in person names collection region
Context.getPersonService().getPerson(name.getPerson().getPersonId());
//Let's have the name in a query cache
sf.getCurrentSession().createCriteria(PERSON_NAME_CLASS)
.add(Restrictions.eq("personNameId", PERSON_NAME_ID_2))
.setCacheable(true)
.setCacheRegion(QUERY_REGION)
.list();
The entity exists in the Cache: because the following assert passes.
To quote from the Hibernate docs: “By default, the statistics are not collected because this incurs an additional processing and memory overhead.” So, most likely, we just haven’t enabled Hibernate statistics… and I probably wouldn’t do that just for this use case. Why do you need those two tests to pass? Just remove them. sf.getCache().containsEntry() seems to be enough to prove whether or not the entity is in the cache.
Those assertions are meant to ensure that query caches are discarded, I think the name second-level-statistics is a little confusing here, they are not really hit/miss statistics. Basically if you comment out the calls to sf.getCache().evictQueryRegions() in your new methods, the tests should fail otherwise should pass
I thought this is done through .setCacheable(true) which I did while creating a query.
Even hibernate.cache.use_second_level_cache is also set to true in core-api
What could be going wrong here??
Is there any other way we could test the existence of query regions??
I thought that may be i needed to restart the context after adding the property with Context.getContextDAO().startup(runtimeProperties).
On trying it, it was throwing an org.openmrs.api.APIException: contextDAO is null yet Context.getContextDAO() returns an org.openmrs.api.db.hibernate.HibernateContextDAO hence not null
Can you trying adding a hibernate.default.properties file in the test resources folder and see if that works? Possibly with just that one property. Though I’d think overriding getRuntimeProperties should work too
After setting the property in the override method, when i check whether it exists runtimeProperties.containsKey("hibernate.cache.use_query_cache"), I get true meaning it exists.
Is there a way i can re-run reload or refresh, in other wards, make the configurations be picked again?
I have tried Context.getContextDAO().startup(runtimeProperties) but it complains that contextDAO is null
I am going to give this one more day of trial. If it doesn’t work out, i will try and see if there is any other way of enabling query-cache other than relaying on the environment variables.