Skip to content

Commit 13ccc8e

Browse files
committed
Merge pull request #723 from svolsky/SPR-12653
* SPR-12653: Synchronize clear on TransactionAwareCacheDecorator
2 parents 6b3092c + ef95fc2 commit 13ccc8e

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

spring-context-support/src/main/java/org/springframework/cache/transaction/TransactionAwareCacheDecorator.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@
2222
import org.springframework.util.Assert;
2323

2424
/**
25-
* Cache decorator which synchronizes its {@link #put} and {@link #evict} operations with
26-
* Spring-managed transactions (through Spring's {@link TransactionSynchronizationManager},
27-
* performing the actual cache put/evict operation only in the after-commit phase of a
28-
* successful transaction. If no transaction is active, {@link #put} and {@link #evict}
29-
* operations will be performed immediately, as usual.
25+
* Cache decorator which synchronizes its {@link #put}, {@link #evict} and {@link #clear}
26+
* operations with Spring-managed transactions (through Spring's {@link TransactionSynchronizationManager},
27+
* performing the actual cache put/evict/clear operation only in the after-commit phase of a
28+
* successful transaction. If no transaction is active, {@link #put}, {@link #evict} and
29+
* {@link #clear} operations will be performed immediately, as usual.
3030
*
3131
* <p>Use of more aggressive operations such as {@link #putIfAbsent} cannot be deferred
3232
* to the after-commit phase of a running transaction. Use these with care.
3333
*
3434
* @author Juergen Hoeller
3535
* @author Stephane Nicoll
36+
* @author Stas Volsky
3637
* @since 3.2
3738
* @see TransactionAwareCacheManagerProxy
3839
*/
@@ -108,7 +109,17 @@ public void afterCommit() {
108109

109110
@Override
110111
public void clear() {
111-
this.targetCache.clear();
112+
if (TransactionSynchronizationManager.isSynchronizationActive()) {
113+
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
114+
@Override
115+
public void afterCommit() {
116+
targetCache.clear();
117+
}
118+
});
119+
}
120+
else {
121+
this.targetCache.clear();
122+
}
112123
}
113124

114125
}

spring-context-support/src/test/java/org/springframework/cache/transaction/TransactionAwareCacheDecoratorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,31 @@ public void evictTransactional() {
128128
assertNull(target.get(key));
129129
}
130130

131+
@Test
132+
public void clearNonTransactional() {
133+
Cache target = new ConcurrentMapCache("testCache");
134+
Cache cache = new TransactionAwareCacheDecorator(target);
135+
Object key = new Object();
136+
cache.put(key, "123");
137+
138+
cache.clear();
139+
assertNull(target.get(key));
140+
}
141+
142+
@Test
143+
public void clearTransactional() {
144+
Cache target = new ConcurrentMapCache("testCache");
145+
Cache cache = new TransactionAwareCacheDecorator(target);
146+
Object key = new Object();
147+
cache.put(key, "123");
148+
149+
150+
TransactionStatus status = txManager.getTransaction(new DefaultTransactionAttribute(
151+
TransactionDefinition.PROPAGATION_REQUIRED));
152+
cache.clear();
153+
assertEquals("123", target.get(key, String.class));
154+
txManager.commit(status);
155+
156+
assertNull(target.get(key));
157+
}
131158
}

0 commit comments

Comments
 (0)