View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.cache;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.apache.ibatis.cache.decorators.TransactionalCache;
22  
23  /**
24   * @author Clinton Begin
25   */
26  public class TransactionalCacheManager {
27  
28    private Map<Cache, TransactionalCache> transactionalCaches = new HashMap<Cache, TransactionalCache>();
29  
30    public void clear(Cache cache) {
31      getTransactionalCache(cache).clear();
32    }
33  
34    public Object getObject(Cache cache, CacheKey key) {
35      return getTransactionalCache(cache).getObject(key);
36    }
37    
38    public void putObject(Cache cache, CacheKey key, Object value) {
39      getTransactionalCache(cache).putObject(key, value);
40    }
41  
42    public void commit() {
43      for (TransactionalCache txCache : transactionalCaches.values()) {
44        txCache.commit();
45      }
46    }
47  
48    public void rollback() {
49      for (TransactionalCache txCache : transactionalCaches.values()) {
50        txCache.rollback();
51      }
52    }
53  
54    private TransactionalCache getTransactionalCache(Cache cache) {
55      TransactionalCache txCache = transactionalCaches.get(cache);
56      if (txCache == null) {
57        txCache = new TransactionalCache(cache);
58        transactionalCaches.put(cache, txCache);
59      }
60      return txCache;
61    }
62  
63  }