1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 return statementMap.keySet().contains(sql) && !statementMap.get(sql).getConnection().isClosed();
99 } catch (SQLException e) {
100 return false;
101 }
102 }
103
104 private Statement getStatement(String s) {
105 return statementMap.get(s);
106 }
107
108 private void putStatement(String sql, Statement stmt) {
109 statementMap.put(sql, stmt);
110 }
111
112 }