|
15 | 15 | */
|
16 | 16 | package org.apache.ibatis.session;
|
17 | 17 |
|
| 18 | +import java.sql.SQLException; |
18 | 19 | import java.util.Arrays;
|
19 | 20 | import java.util.Collection;
|
20 | 21 | import java.util.HashMap;
|
|
23 | 24 | import java.util.LinkedList;
|
24 | 25 | import java.util.List;
|
25 | 26 | import java.util.Map;
|
| 27 | +import java.util.Objects; |
26 | 28 | import java.util.Properties;
|
27 | 29 | import java.util.Set;
|
28 | 30 | import java.util.concurrent.ConcurrentHashMap;
|
@@ -831,7 +833,11 @@ public boolean hasParameterMap(String id) {
|
831 | 833 | }
|
832 | 834 |
|
833 | 835 | public void addMappedStatement(MappedStatement ms) {
|
834 |
| - mappedStatements.put(ms.getId(), ms); |
| 836 | + String id = ms.getId(); |
| 837 | + if (this.getSupportDynamicRoutingDataSource() && Objects.nonNull(ms.getDatabaseId())) { |
| 838 | + id = id + "#" + ms.getDatabaseId(); |
| 839 | + } |
| 840 | + mappedStatements.put(id, ms); |
835 | 841 | }
|
836 | 842 |
|
837 | 843 | public Collection<String> getMappedStatementNames() {
|
@@ -920,7 +926,22 @@ public MappedStatement getMappedStatement(String id, boolean validateIncompleteS
|
920 | 926 | if (validateIncompleteStatements) {
|
921 | 927 | buildAllStatements();
|
922 | 928 | }
|
923 |
| - return mappedStatements.get(id); |
| 929 | + MappedStatement statement = mappedStatements.get(this.getMappedStatementId(id)); |
| 930 | + if (this.getSupportDynamicRoutingDataSource() && Objects.isNull(statement)) { |
| 931 | + statement = mappedStatements.get(id); |
| 932 | + } |
| 933 | + return statement; |
| 934 | + } |
| 935 | + |
| 936 | + protected String getMappedStatementId(String id) { |
| 937 | + try { |
| 938 | + String databaseId = this.getCurrentDatabaseId(); |
| 939 | + if (this.getSupportDynamicRoutingDataSource() && Objects.nonNull(databaseId)) { |
| 940 | + return id + "#" + databaseId; |
| 941 | + } |
| 942 | + } catch (SQLException ignore) { |
| 943 | + } |
| 944 | + return id; |
924 | 945 | }
|
925 | 946 |
|
926 | 947 | public Map<String, XNode> getSqlFragments() {
|
@@ -959,7 +980,8 @@ public boolean hasStatement(String statementName, boolean validateIncompleteStat
|
959 | 980 | if (validateIncompleteStatements) {
|
960 | 981 | buildAllStatements();
|
961 | 982 | }
|
962 |
| - return mappedStatements.containsKey(statementName); |
| 983 | + return mappedStatements.containsKey(this.getMappedStatementId(statementName)) |
| 984 | + || this.getSupportDynamicRoutingDataSource() && mappedStatements.containsKey(statementName); |
963 | 985 | }
|
964 | 986 |
|
965 | 987 | public void addCacheRef(String namespace, String referencedNamespace) {
|
|
0 commit comments