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,
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 }