| Local query caches are not populated if the transaction is not committed. The same test case works on hibernate 4.3.11 Our scenario: Read-only services that only execute queries and never commit the transaction. It should still be possible to use/configure hibernate/infinispan to cache the results, regardless the transaction state. The test case:
@Test
public void resultsShouldBeCachedOnRollback() throws Exception {
MyEntity entity = new MyEntity();
entity.setId(1);
entity.setName("my entity");
beginTransaction();
entityManager.persist(entity);
entityManager.flush();
commitTransaction();
Thread.sleep(1500);
beginTransaction();
TypedQuery<MyEntity> query = createReadAllQuery();
List<MyEntity> results = query.getResultList();
assertFalse(results.isEmpty());
rollbackTransaction();
beginTransaction();
try (PreparedStatement ps = entityManager.unwrap(SessionImplementor.class).connection()
.prepareStatement("delete from MyEntity")) {
ps.execute();
}
commitTransaction();
beginTransaction();
query = createReadAllQuery();
results = query.getResultList();
assertFalse(results.isEmpty());
rollbackTransaction();
}
<local-cache name="local-query">
<!--
We changed this to NONE so that rolled back transactions still allow to put data into the query cache: https://hibernate.atlassian.net/browse/HHH-9890
-->
<transaction mode="NONE"/>
<eviction strategy="NONE"/>
</local-cache>
<local-cache name="timestamps">
<transaction mode="NONE"/>
<eviction strategy="NONE"/>
</local-cache>
Test case is attached: MyApp3 Test case class: com.myapp.NonTransactionalQueryCacheTest |