Skip to content

Commit

Permalink
First attempt for fixing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
neiser committed Jun 2, 2022
1 parent 2269c00 commit cbc673d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.springframework.cache.transaction;

import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.lang.Nullable;

/**
* Base class for CacheManager implementations that want to support built-in
Expand All @@ -33,7 +35,8 @@
public abstract class AbstractTransactionSupportingCacheManager extends AbstractCacheManager {

private boolean transactionAware = false;

@Nullable
private CacheErrorHandler cacheErrorHandler;

/**
* Set whether this CacheManager should expose transaction-aware Cache objects.
Expand All @@ -52,10 +55,15 @@ public boolean isTransactionAware() {
return this.transactionAware;
}

// TODO How to integrate CachingCustomizer's errorHandler here?
public void setCacheErrorHandler(@Nullable CacheErrorHandler cacheErrorHandler) {
this.cacheErrorHandler = cacheErrorHandler;
}

@Override
protected Cache decorateCache(Cache cache) {
return (isTransactionAware() ? new TransactionAwareCacheDecorator(cache) : cache);
return (isTransactionAware() ? new TransactionAwareCacheDecorator(cache, cacheErrorHandler) : cache);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package org.springframework.cache.transaction;

import java.util.concurrent.Callable;

import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.lang.Nullable;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;

import java.util.concurrent.Callable;

/**
* Cache decorator which synchronizes its {@link #put}, {@link #evict} and
* {@link #clear} operations with Spring-managed transactions (through Spring's
Expand All @@ -39,21 +40,32 @@
* @author Juergen Hoeller
* @author Stephane Nicoll
* @author Stas Volsky
* @since 3.2
* @see TransactionAwareCacheManagerProxy
* @since 3.2
*/
public class TransactionAwareCacheDecorator implements Cache {

private final Cache targetCache;

@Nullable
private final CacheErrorHandler cacheErrorHandler;


public TransactionAwareCacheDecorator(Cache targetCache) {
this(targetCache, null);
}


/**
* Create a new TransactionAwareCache for the given target Cache.
* @param targetCache the target Cache to decorate
*
* @param targetCache the target Cache to decorate
* @param cacheErrorHandler error handler used when deferred cache action fails (can be null)
*/
public TransactionAwareCacheDecorator(Cache targetCache) {
public TransactionAwareCacheDecorator(Cache targetCache, @Nullable CacheErrorHandler cacheErrorHandler) {
Assert.notNull(targetCache, "Target Cache must not be null");
this.targetCache = targetCache;
this.cacheErrorHandler = cacheErrorHandler;
}


Expand Down Expand Up @@ -81,6 +93,7 @@ public ValueWrapper get(Object key) {
}

@Override
@Nullable
public <T> T get(Object key, @Nullable Class<T> type) {
return this.targetCache.get(key, type);
}
Expand All @@ -97,7 +110,14 @@ public void put(final Object key, @Nullable final Object value) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
TransactionAwareCacheDecorator.this.targetCache.put(key, value);
try {
TransactionAwareCacheDecorator.this.targetCache.put(key, value);
} catch (RuntimeException e) {
if (cacheErrorHandler != null) {
cacheErrorHandler.handleCachePutError(e, targetCache, key, value);
}
throw e;
}
}
});
}
Expand All @@ -118,7 +138,14 @@ public void evict(final Object key) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
TransactionAwareCacheDecorator.this.targetCache.evict(key);
try {
TransactionAwareCacheDecorator.this.targetCache.evict(key);
} catch (RuntimeException e) {
if (cacheErrorHandler != null) {
cacheErrorHandler.handleCacheEvictError(e, targetCache, key);
}
throw e;
}
}
});
}
Expand All @@ -138,7 +165,14 @@ public void clear() {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
@Override
public void afterCommit() {
targetCache.clear();
try {
targetCache.clear();
} catch (RuntimeException e) {
if (cacheErrorHandler != null) {
cacheErrorHandler.handleCacheClearError(e, targetCache);
}
throw e;
}
}
});
}
Expand Down

0 comments on commit cbc673d

Please sign in to comment.