Non transactional query cache only cache results if transaction is committed
Description
Attachments
1
- 21 Mar 2017, 01:14 PM
Activity
Show:
Galder Zamarreno July 12, 2018 at 1:24 PM
I've verified the issue and it's still present in latest Infinispan cache provider. The provider now lives within Infinispan, so I've cloned this issue to ISPN-9369. I'll close this one for now.
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"); // Step 1: Create a new record beginTransaction(); entityManager.persist(entity); entityManager.flush(); commitTransaction(); // 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(); }
<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