Skip to content

Commit 6a59687

Browse files
committed
initialize all of the statements which is marked datasourceId when configuration set supportDynamicRoutingDataSource.
The databaseIdProvider provide current databaseid for statement match.
1 parent 1a145cd commit 6a59687

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.apache.ibatis.session;
1717

18+
import java.sql.SQLException;
1819
import java.util.Arrays;
1920
import java.util.Collection;
2021
import java.util.HashMap;
@@ -23,6 +24,7 @@
2324
import java.util.LinkedList;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Objects;
2628
import java.util.Properties;
2729
import java.util.Set;
2830
import java.util.concurrent.ConcurrentHashMap;
@@ -831,7 +833,11 @@ public boolean hasParameterMap(String id) {
831833
}
832834

833835
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);
835841
}
836842

837843
public Collection<String> getMappedStatementNames() {
@@ -920,7 +926,22 @@ public MappedStatement getMappedStatement(String id, boolean validateIncompleteS
920926
if (validateIncompleteStatements) {
921927
buildAllStatements();
922928
}
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;
924945
}
925946

926947
public Map<String, XNode> getSqlFragments() {
@@ -959,7 +980,8 @@ public boolean hasStatement(String statementName, boolean validateIncompleteStat
959980
if (validateIncompleteStatements) {
960981
buildAllStatements();
961982
}
962-
return mappedStatements.containsKey(statementName);
983+
return mappedStatements.containsKey(this.getMappedStatementId(statementName))
984+
|| this.getSupportDynamicRoutingDataSource() && mappedStatements.containsKey(statementName);
963985
}
964986

965987
public void addCacheRef(String namespace, String referencedNamespace) {

0 commit comments

Comments
 (0)