View Javadoc
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.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,
57        BoundSql boundSql) throws SQLException {
58      Configuration configuration = ms.getConfiguration();
59      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler,
60          boundSql);
61      Statement stmt = prepareStatement(handler, ms.getStatementLog());
62      return handler.query(stmt, resultHandler);
63    }
64  
65    @Override
66    protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql)
67        throws SQLException {
68      Configuration configuration = ms.getConfiguration();
69      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
70      Statement stmt = prepareStatement(handler, ms.getStatementLog());
71      return handler.queryCursor(stmt);
72    }
73  
74    @Override
75    public List<BatchResult> doFlushStatements(boolean isRollback) {
76      for (Statement stmt : statementMap.values()) {
77        closeStatement(stmt);
78      }
79      statementMap.clear();
80      return Collections.emptyList();
81    }
82  
83    private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
84      Statement stmt;
85      BoundSql boundSql = handler.getBoundSql();
86      String sql = boundSql.getSql();
87      if (hasStatementFor(sql)) {
88        stmt = getStatement(sql);
89        applyTransactionTimeout(stmt);
90      } else {
91        Connection connection = getConnection(statementLog);
92        stmt = handler.prepare(connection, transaction.getTimeout());
93        putStatement(sql, stmt);
94      }
95      handler.parameterize(stmt);
96      return stmt;
97    }
98  
99    private boolean hasStatementFor(String sql) {
100     try {
101       Statement statement = statementMap.get(sql);
102       return statement != null && !statement.getConnection().isClosed();
103     } catch (SQLException e) {
104       return false;
105     }
106   }
107 
108   private Statement getStatement(String s) {
109     return statementMap.get(s);
110   }
111 
112   private void putStatement(String sql, Statement stmt) {
113     statementMap.put(sql, stmt);
114   }
115 
116 }