Synchronize clear on TransactionAwareCacheDecorator
Previously, a cache decorated with TransactionAwareCacheDecorator would clear the cache immediately, even when a transaction is running. This commit updates the decorator to synchronize to the afterCommit phase for the clear operation as well. Issue: SPR-12653
This commit is contained in:
committed by
Stephane Nicoll
parent
6b3092c236
commit
ef95fc2f7e
@@ -22,17 +22,18 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Cache decorator which synchronizes its {@link #put} and {@link #evict} operations with
|
||||
* Spring-managed transactions (through Spring's {@link TransactionSynchronizationManager},
|
||||
* performing the actual cache put/evict operation only in the after-commit phase of a
|
||||
* successful transaction. If no transaction is active, {@link #put} and {@link #evict}
|
||||
* operations will be performed immediately, as usual.
|
||||
* Cache decorator which synchronizes its {@link #put}, {@link #evict} and {@link #clear}
|
||||
* operations with Spring-managed transactions (through Spring's {@link TransactionSynchronizationManager},
|
||||
* performing the actual cache put/evict/clear operation only in the after-commit phase of a
|
||||
* successful transaction. If no transaction is active, {@link #put}, {@link #evict} and
|
||||
* {@link #clear} operations will be performed immediately, as usual.
|
||||
*
|
||||
* <p>Use of more aggressive operations such as {@link #putIfAbsent} cannot be deferred
|
||||
* to the after-commit phase of a running transaction. Use these with care.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Stephane Nicoll
|
||||
* @author Stas Volsky
|
||||
* @since 3.2
|
||||
* @see TransactionAwareCacheManagerProxy
|
||||
*/
|
||||
@@ -108,7 +109,17 @@ public class TransactionAwareCacheDecorator implements Cache {
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.targetCache.clear();
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) {
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
|
||||
@Override
|
||||
public void afterCommit() {
|
||||
targetCache.clear();
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.targetCache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user