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: {code:java} @Test public void resultsShouldBeCachedOnRollback() throws Exception { MyEntity entity = new MyEntity(); entity.setId(1); entity.setName("my entity");
// Step 1: Create a new record beginTransaction(); entityManager.persist(entity); entityManager.flush(); commitTransaction();
Thread.sleep(1500);
// Step 2: Execute query to cache results, then rollback the transaction beginTransaction(); TypedQuery<MyEntity> query = createReadAllQuery(); List<MyEntity> results = query.getResultList(); assertFalse(results.isEmpty()); rollbackTransaction();
// Step 3: Use jdbc to delete the record (bypassing second level cache // invalidation) beginTransaction(); try (PreparedStatement ps = entityManager.unwrap(SessionImplementor.class).connection() .prepareStatement("delete from MyEntity")) { ps.execute(); } commitTransaction();
// Step 4: Re-execute the query. Second level cache hit and the same // results from step 2 should be returned beginTransaction(); query = createReadAllQuery(); results = query.getResultList(); assertFalse(results.isEmpty()); rollbackTransaction(); } {code}
{code:xml} <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> {code}
Test case is attached: MyApp3 Test case class: com.myapp.NonTransactionalQueryCacheTest |
|