16
16
17
17
package org .springframework .cache .transaction ;
18
18
19
- import java .util .concurrent .Callable ;
20
-
21
19
import org .springframework .cache .Cache ;
20
+ import org .springframework .cache .interceptor .CacheErrorHandler ;
22
21
import org .springframework .lang .Nullable ;
23
22
import org .springframework .transaction .support .TransactionSynchronization ;
24
23
import org .springframework .transaction .support .TransactionSynchronizationManager ;
25
24
import org .springframework .util .Assert ;
26
25
26
+ import java .util .concurrent .Callable ;
27
+
27
28
/**
28
29
* Cache decorator which synchronizes its {@link #put}, {@link #evict} and
29
30
* {@link #clear} operations with Spring-managed transactions (through Spring's
39
40
* @author Juergen Hoeller
40
41
* @author Stephane Nicoll
41
42
* @author Stas Volsky
42
- * @since 3.2
43
43
* @see TransactionAwareCacheManagerProxy
44
+ * @since 3.2
44
45
*/
45
46
public class TransactionAwareCacheDecorator implements Cache {
46
47
47
48
private final Cache targetCache ;
48
49
50
+ @ Nullable
51
+ private final CacheErrorHandler cacheErrorHandler ;
52
+
53
+
54
+ public TransactionAwareCacheDecorator (Cache targetCache ) {
55
+ this (targetCache , null );
56
+ }
57
+
49
58
50
59
/**
51
60
* Create a new TransactionAwareCache for the given target Cache.
52
- * @param targetCache the target Cache to decorate
61
+ *
62
+ * @param targetCache the target Cache to decorate
63
+ * @param cacheErrorHandler error handler used when deferred cache action fails (can be null)
53
64
*/
54
- public TransactionAwareCacheDecorator (Cache targetCache ) {
65
+ public TransactionAwareCacheDecorator (Cache targetCache , @ Nullable CacheErrorHandler cacheErrorHandler ) {
55
66
Assert .notNull (targetCache , "Target Cache must not be null" );
56
67
this .targetCache = targetCache ;
68
+ this .cacheErrorHandler = cacheErrorHandler ;
57
69
}
58
70
59
71
@@ -81,6 +93,7 @@ public ValueWrapper get(Object key) {
81
93
}
82
94
83
95
@ Override
96
+ @ Nullable
84
97
public <T > T get (Object key , @ Nullable Class <T > type ) {
85
98
return this .targetCache .get (key , type );
86
99
}
@@ -97,7 +110,14 @@ public void put(final Object key, @Nullable final Object value) {
97
110
TransactionSynchronizationManager .registerSynchronization (new TransactionSynchronization () {
98
111
@ Override
99
112
public void afterCommit () {
100
- TransactionAwareCacheDecorator .this .targetCache .put (key , value );
113
+ try {
114
+ TransactionAwareCacheDecorator .this .targetCache .put (key , value );
115
+ } catch (RuntimeException e ) {
116
+ if (cacheErrorHandler != null ) {
117
+ cacheErrorHandler .handleCachePutError (e , targetCache , key , value );
118
+ }
119
+ throw e ;
120
+ }
101
121
}
102
122
});
103
123
}
@@ -118,7 +138,14 @@ public void evict(final Object key) {
118
138
TransactionSynchronizationManager .registerSynchronization (new TransactionSynchronization () {
119
139
@ Override
120
140
public void afterCommit () {
121
- TransactionAwareCacheDecorator .this .targetCache .evict (key );
141
+ try {
142
+ TransactionAwareCacheDecorator .this .targetCache .evict (key );
143
+ } catch (RuntimeException e ) {
144
+ if (cacheErrorHandler != null ) {
145
+ cacheErrorHandler .handleCacheEvictError (e , targetCache , key );
146
+ }
147
+ throw e ;
148
+ }
122
149
}
123
150
});
124
151
}
@@ -138,7 +165,14 @@ public void clear() {
138
165
TransactionSynchronizationManager .registerSynchronization (new TransactionSynchronization () {
139
166
@ Override
140
167
public void afterCommit () {
141
- targetCache .clear ();
168
+ try {
169
+ targetCache .clear ();
170
+ } catch (RuntimeException e ) {
171
+ if (cacheErrorHandler != null ) {
172
+ cacheErrorHandler .handleCacheClearError (e , targetCache );
173
+ }
174
+ throw e ;
175
+ }
142
176
}
143
177
});
144
178
}
0 commit comments