We're updating the issue view to help you get more done. 

Non transactional query cache only cache results if transaction is committed

Description

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @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(); }
1 2 3 4 5 6 7 8 9 10 11 <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

Environment

JPA/Hibernate + Spring + JTA + Infinispan

Status

Assignee

Galder Zamarreno

Reporter

Jones

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Worked in

4.3.11

Components

Affects versions

5.1.5
5.0.12
5.2.9

Priority

Major