001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.transaction; 018 019import org.springframework.cache.Cache; 020import org.springframework.cache.support.AbstractCacheManager; 021 022/** 023 * Base class for CacheManager implementations that want to support built-in 024 * awareness of Spring-managed transactions. This usually needs to be switched 025 * on explicitly through the {@link #setTransactionAware} bean property. 026 * 027 * @author Juergen Hoeller 028 * @since 3.2 029 * @see #setTransactionAware 030 * @see TransactionAwareCacheDecorator 031 * @see TransactionAwareCacheManagerProxy 032 */ 033public abstract class AbstractTransactionSupportingCacheManager extends AbstractCacheManager { 034 035 private boolean transactionAware = false; 036 037 038 /** 039 * Set whether this CacheManager should expose transaction-aware Cache objects. 040 * <p>Default is "false". Set this to "true" to synchronize cache put/evict 041 * operations with ongoing Spring-managed transactions, performing the actual cache 042 * put/evict operation only in the after-commit phase of a successful transaction. 043 */ 044 public void setTransactionAware(boolean transactionAware) { 045 this.transactionAware = transactionAware; 046 } 047 048 /** 049 * Return whether this CacheManager has been configured to be transaction-aware. 050 */ 051 public boolean isTransactionAware() { 052 return this.transactionAware; 053 } 054 055 056 @Override 057 protected Cache decorateCache(Cache cache) { 058 return (isTransactionAware() ? new TransactionAwareCacheDecorator(cache) : cache); 059 } 060 061}