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.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.executor.statement.StatementHandler;
27  import org.apache.ibatis.logging.Log;
28  import org.apache.ibatis.mapping.BoundSql;
29  import org.apache.ibatis.mapping.MappedStatement;
30  import org.apache.ibatis.session.Configuration;
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   */
38  public class ReuseExecutor extends BaseExecutor {
39  
40    private final Map<String, Statement> statementMap = new HashMap<String, Statement>();
41  
42    public ReuseExecutor(Configuration configuration, Transaction transaction) {
43      super(configuration, transaction);
44    }
45  
46    @Override
47    public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
48      Configuration configuration = ms.getConfiguration();
49      StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
50      Statement stmt = prepareStatement(handler, ms.getStatementLog());
51      return handler.update(stmt);
52    }
53  
54    @Override
55    public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
56      Configuration configuration = ms.getConfiguration();
57      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
58      Statement stmt = prepareStatement(handler, ms.getStatementLog());
59      return handler.<E>query(stmt, resultHandler);
60    }
61  
62    @Override
63    public List<BatchResult> doFlushStatements(boolean isRollback) throws SQLException {
64      for (Statement stmt : statementMap.values()) {
65        closeStatement(stmt);
66      }
67      statementMap.clear();
68      return Collections.emptyList();
69    }
70  
71    private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
72      Statement stmt;
73      BoundSql boundSql = handler.getBoundSql();
74      String sql = boundSql.getSql();
75      if (hasStatementFor(sql)) {
76        stmt = getStatement(sql);
77      } else {
78        Connection connection = getConnection(statementLog);
79        stmt = handler.prepare(connection);
80        putStatement(sql, stmt);
81      }
82      handler.parameterize(stmt);
83      return stmt;
84    }
85  
86    private boolean hasStatementFor(String sql) {
87      try {
88        return statementMap.keySet().contains(sql) && !statementMap.get(sql).getConnection().isClosed();
89      } catch (SQLException e) {
90        return false;
91      }
92    }
93  
94    private Statement getStatement(String s) {
95      return statementMap.get(s);
96    }
97  
98    private void putStatement(String sql, Statement stmt) {
99      statementMap.put(sql, stmt);
100   }
101 
102 }