Skip to content

Commit ab4c684

Browse files
committed
HHH-10005 Query in context needs precedence over query in cache
1 parent 44e7171 commit ab4c684

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/query/QueryResultsRegionImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,17 @@ public Object get(SessionImplementor session, Object key) throws CacheException
123123
// to avoid holding locks that would prevent updates.
124124
// Add a zero (or low) timeout option so we don't block
125125
// waiting for tx's that did a put to commit
126-
Object result;
127-
if ( skipCacheStore ) {
128-
result = getCache.withFlags( Flag.SKIP_CACHE_STORE ).get( key );
129-
}
130-
else {
131-
result = getCache.get( key );
126+
Object result = null;
127+
Map map = transactionContext.get(session);
128+
if (map != null) {
129+
result = map.get(key);
132130
}
133131
if (result == null) {
134-
Map map = transactionContext.get(session);
135-
if (map != null) {
136-
result = map.get(key);
132+
if ( skipCacheStore ) {
133+
result = getCache.withFlags( Flag.SKIP_CACHE_STORE ).get( key );
134+
}
135+
else {
136+
result = getCache.get( key );
137137
}
138138
}
139139
return result;

hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/query/QueryRegionImplTestCase.java

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212
import java.util.Properties;
1313
import java.util.concurrent.CountDownLatch;
14+
import java.util.concurrent.CyclicBarrier;
1415
import java.util.concurrent.TimeUnit;
1516

1617
import org.hibernate.Session;
@@ -102,7 +103,7 @@ public void testPutDoesNotBlockGet() throws Exception {
102103
@Override
103104
public void run() {
104105
try {
105-
assertNotEquals(VALUE2, callWithSession(sessionFactory, session-> region.get(session, KEY)));
106+
assertNotEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
106107
} catch (AssertionFailedError e) {
107108
holder.addAssertionFailure(e);
108109
} catch (Exception e) {
@@ -118,7 +119,7 @@ public void run() {
118119
public void run() {
119120
try {
120121
withSession(sessionFactory, session -> {
121-
region.put((SessionImplementor) session, KEY, VALUE2);
122+
region.put(session, KEY, VALUE2);
122123
writerLatch.await();
123124
});
124125
} catch (Exception e) {
@@ -143,7 +144,7 @@ public void run() {
143144

144145
assertTrue("Reader finished promptly", completionLatch.await(100, TimeUnit.MILLISECONDS));
145146

146-
assertEquals(VALUE2, region.get(null, KEY));
147+
assertEquals(VALUE2, callWithSession(sessionFactory, session -> region.get(session, KEY)));
147148
});
148149
}
149150

@@ -313,6 +314,71 @@ public void run() {
313314
});
314315
}
315316

317+
@Test
318+
public void testQueryUpdate() throws Exception {
319+
withQueryRegion((sessionFactory, region) -> {
320+
ExceptionHolder holder = new ExceptionHolder();
321+
CyclicBarrier barrier = new CyclicBarrier(2);
322+
withSession(sessionFactory, session -> region.put(session, KEY, VALUE1));
323+
324+
Thread updater = new Thread() {
325+
@Override
326+
public void run() {
327+
try {
328+
withSession(sessionFactory, (session) -> {
329+
assertEquals(VALUE1, region.get(session, KEY));
330+
region.put(session, KEY, VALUE2);
331+
assertEquals(VALUE2, region.get(session, KEY));
332+
barrier.await(5, TimeUnit.SECONDS);
333+
barrier.await(5, TimeUnit.SECONDS);
334+
region.put(session, KEY, VALUE3);
335+
assertEquals(VALUE3, region.get(session, KEY));
336+
barrier.await(5, TimeUnit.SECONDS);
337+
barrier.await(5, TimeUnit.SECONDS);
338+
});
339+
} catch (AssertionFailedError e) {
340+
holder.addAssertionFailure(e);
341+
barrier.reset();
342+
} catch (Exception e) {
343+
holder.addException(e);
344+
barrier.reset();
345+
}
346+
}
347+
};
348+
349+
Thread reader = new Thread() {
350+
@Override
351+
public void run() {
352+
try {
353+
withSession(sessionFactory, (session) -> {
354+
assertEquals(VALUE1, region.get(session, KEY));
355+
barrier.await(5, TimeUnit.SECONDS);
356+
assertEquals(VALUE1, region.get(session, KEY));
357+
barrier.await(5, TimeUnit.SECONDS);
358+
barrier.await(5, TimeUnit.SECONDS);
359+
assertEquals(VALUE1, region.get(session, KEY));
360+
barrier.await(5, TimeUnit.SECONDS);
361+
});
362+
} catch (AssertionFailedError e) {
363+
holder.addAssertionFailure(e);
364+
barrier.reset();
365+
} catch (Exception e) {
366+
holder.addException(e);
367+
barrier.reset();
368+
}
369+
}
370+
};
371+
372+
updater.start();
373+
reader.start();
374+
updater.join();
375+
reader.join();
376+
holder.checkExceptions();
377+
378+
assertEquals(VALUE3, callWithSession(sessionFactory, session -> region.get(session, KEY)));
379+
});
380+
}
381+
316382
@Listener
317383
public class GetBlocker {
318384
private final CountDownLatch latch;

0 commit comments

Comments
 (0)