• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / mybatis-3 / #2968

pending completion
#2968

push

github

web-flow
Merge pull request #2792 from hazendaz/formatting

[ci] Formatting

84 of 84 new or added lines in 23 files covered. (100.0%)

9412 of 10781 relevant lines covered (87.3%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.29
/src/main/java/org/apache/ibatis/executor/CachingExecutor.java
1
/*
2
 *    Copyright 2009-2023 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
 *       https://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.executor;
17

18
import java.sql.SQLException;
19
import java.util.List;
20

21
import org.apache.ibatis.cache.Cache;
22
import org.apache.ibatis.cache.CacheKey;
23
import org.apache.ibatis.cache.TransactionalCacheManager;
24
import org.apache.ibatis.cursor.Cursor;
25
import org.apache.ibatis.mapping.BoundSql;
26
import org.apache.ibatis.mapping.MappedStatement;
27
import org.apache.ibatis.mapping.ParameterMapping;
28
import org.apache.ibatis.mapping.ParameterMode;
29
import org.apache.ibatis.mapping.StatementType;
30
import org.apache.ibatis.reflection.MetaObject;
31
import org.apache.ibatis.session.ResultHandler;
32
import org.apache.ibatis.session.RowBounds;
33
import org.apache.ibatis.transaction.Transaction;
34

35
/**
36
 * @author Clinton Begin
37
 * @author Eduardo Macarron
38
 */
39
public class CachingExecutor implements Executor {
40

41
  private final Executor delegate;
42
  private final TransactionalCacheManager tcm = new TransactionalCacheManager();
1✔
43

44
  public CachingExecutor(Executor delegate) {
1✔
45
    this.delegate = delegate;
1✔
46
    delegate.setExecutorWrapper(this);
1✔
47
  }
1✔
48

49
  @Override
50
  public Transaction getTransaction() {
51
    return delegate.getTransaction();
1✔
52
  }
53

54
  @Override
55
  public void close(boolean forceRollback) {
56
    try {
57
      // issues #499, #524 and #573
58
      if (forceRollback) {
1✔
59
        tcm.rollback();
1✔
60
      } else {
61
        tcm.commit();
1✔
62
      }
63
    } finally {
64
      delegate.close(forceRollback);
1✔
65
    }
66
  }
1✔
67

68
  @Override
69
  public boolean isClosed() {
70
    return delegate.isClosed();
1✔
71
  }
72

73
  @Override
74
  public int update(MappedStatement ms, Object parameterObject) throws SQLException {
75
    flushCacheIfRequired(ms);
1✔
76
    return delegate.update(ms, parameterObject);
1✔
77
  }
78

79
  @Override
80
  public <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException {
81
    flushCacheIfRequired(ms);
1✔
82
    return delegate.queryCursor(ms, parameter, rowBounds);
1✔
83
  }
84

85
  @Override
86
  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler)
87
      throws SQLException {
88
    BoundSql boundSql = ms.getBoundSql(parameterObject);
1✔
89
    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);
1✔
90
    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
1✔
91
  }
92

93
  @Override
94
  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler,
95
      CacheKey key, BoundSql boundSql) throws SQLException {
96
    Cache cache = ms.getCache();
1✔
97
    if (cache != null) {
1✔
98
      flushCacheIfRequired(ms);
1✔
99
      if (ms.isUseCache() && resultHandler == null) {
1✔
100
        ensureNoOutParams(ms, boundSql);
1✔
101
        @SuppressWarnings("unchecked")
102
        List<E> list = (List<E>) tcm.getObject(cache, key);
1✔
103
        if (list == null) {
1✔
104
          list = delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
1✔
105
          tcm.putObject(cache, key, list); // issue #578 and #116
1✔
106
        }
107
        return list;
1✔
108
      }
109
    }
110
    return delegate.query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);
1✔
111
  }
112

113
  @Override
114
  public List<BatchResult> flushStatements() throws SQLException {
115
    return delegate.flushStatements();
1✔
116
  }
117

118
  @Override
119
  public void commit(boolean required) throws SQLException {
120
    delegate.commit(required);
1✔
121
    tcm.commit();
1✔
122
  }
1✔
123

124
  @Override
125
  public void rollback(boolean required) throws SQLException {
126
    try {
127
      delegate.rollback(required);
1✔
128
    } finally {
129
      if (required) {
1✔
130
        tcm.rollback();
1✔
131
      }
132
    }
133
  }
1✔
134

135
  private void ensureNoOutParams(MappedStatement ms, BoundSql boundSql) {
136
    if (ms.getStatementType() == StatementType.CALLABLE) {
1✔
137
      for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
×
138
        if (parameterMapping.getMode() != ParameterMode.IN) {
×
139
          throw new ExecutorException(
×
140
              "Caching stored procedures with OUT params is not supported.  Please configure useCache=false in "
141
                  + ms.getId() + " statement.");
×
142
        }
143
      }
×
144
    }
145
  }
1✔
146

147
  @Override
148
  public CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql) {
149
    return delegate.createCacheKey(ms, parameterObject, rowBounds, boundSql);
1✔
150
  }
151

152
  @Override
153
  public boolean isCached(MappedStatement ms, CacheKey key) {
154
    return delegate.isCached(ms, key);
1✔
155
  }
156

157
  @Override
158
  public void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key,
159
      Class<?> targetType) {
160
    delegate.deferLoad(ms, resultObject, property, key, targetType);
1✔
161
  }
1✔
162

163
  @Override
164
  public void clearLocalCache() {
165
    delegate.clearLocalCache();
1✔
166
  }
1✔
167

168
  private void flushCacheIfRequired(MappedStatement ms) {
169
    Cache cache = ms.getCache();
1✔
170
    if (cache != null && ms.isFlushCacheRequired()) {
1✔
171
      tcm.clear(cache);
1✔
172
    }
173
  }
1✔
174

175
  @Override
176
  public void setExecutorWrapper(Executor executor) {
177
    throw new UnsupportedOperationException("This method should not be called");
×
178
  }
179

180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc