View Javadoc
1   /*
2    *    Copyright 2009-2021 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.executor;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.ibatis.cursor.Cursor;
27  import org.apache.ibatis.executor.statement.StatementHandler;
28  import org.apache.ibatis.logging.Log;
29  import org.apache.ibatis.mapping.BoundSql;
30  import org.apache.ibatis.mapping.MappedStatement;
31  import org.apache.ibatis.session.Configuration;
32  import org.apache.ibatis.session.ResultHandler;
33  import org.apache.ibatis.session.RowBounds;
34  import org.apache.ibatis.transaction.Transaction;
35  
36  /**
37   * @author Clinton Begin
38   */
39  public class ReuseExecutor extends BaseExecutor {
40  
41    private final Map<String, Statement> statementMap = new HashMap<>();
42  
43    public ReuseExecutor(Configuration configuration, Transaction transaction) {
44      super(configuration, transaction);
45    }
46  
47    @Override
48    public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
49      Configuration configuration = ms.getConfiguration();
50      StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
51      Statement stmt = prepareStatement(handler, ms.getStatementLog());
52      return handler.update(stmt);
53    }
54  
55    @Override
56    public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
57      Configuration configuration = ms.getConfiguration();
58      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
59      Statement stmt = prepareStatement(handler, ms.getStatementLog());
60      return handler.query(stmt, resultHandler);
61    }
62  
63    @Override
64    protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
65      Configuration configuration = ms.getConfiguration();
66      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
67      Statement stmt = prepareStatement(handler, ms.getStatementLog());
68      return handler.queryCursor(stmt);
69    }
70  
71    @Override
72    public List<BatchResult> doFlushStatements(boolean isRollback) {
73      for (Statement stmt : statementMap.values()) {
74        closeStatement(stmt);
75      }
76      statementMap.clear();
77      return Collections.emptyList();
78    }
79  
80    private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
81      Statement stmt;
82      BoundSql boundSql = handler.getBoundSql();
83      String sql = boundSql.getSql();
84      if (hasStatementFor(sql)) {
85        stmt = getStatement(sql);
86        applyTransactionTimeout(stmt);
87      } else {
88        Connection connection = getConnection(statementLog);
89        stmt = handler.prepare(connection, transaction.getTimeout());
90        putStatement(sql, stmt);
91      }
92      handler.parameterize(stmt);
93      return stmt;
94    }
95  
96    private boolean hasStatementFor(String sql) {
97      try {
98        Statement statement = statementMap.get(sql);
99        return statement != null && !statement.getConnection().isClosed();
100     } catch (SQLException e) {
101       return false;
102     }
103   }
104 
105   private Statement getStatement(String s) {
106     return statementMap.get(s);
107   }
108 
109   private void putStatement(String sql, Statement stmt) {
110     statementMap.put(sql, stmt);
111   }
112 
113 }