|
22 | 22 | import org.springframework.util.Assert;
|
23 | 23 |
|
24 | 24 | /**
|
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. |
30 | 30 | *
|
31 | 31 | * <p>Use of more aggressive operations such as {@link #putIfAbsent} cannot be deferred
|
32 | 32 | * to the after-commit phase of a running transaction. Use these with care.
|
33 | 33 | *
|
34 | 34 | * @author Juergen Hoeller
|
35 | 35 | * @author Stephane Nicoll
|
| 36 | + * @author Stas Volsky |
36 | 37 | * @since 3.2
|
37 | 38 | * @see TransactionAwareCacheManagerProxy
|
38 | 39 | */
|
@@ -108,7 +109,17 @@ public void afterCommit() {
|
108 | 109 |
|
109 | 110 | @Override
|
110 | 111 | 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 | + } |
112 | 123 | }
|
113 | 124 |
|
114 | 125 | }
|
0 commit comments