Skip to content

Commit 34fd2bd

Browse files
authored
Merge pull request mybatis#3146 from nieqiurong/shared-ambiguity
Shared ambiguity instance
2 parents 8ac3920 + ad8c4d0 commit 34fd2bd

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

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

+19-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2024 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,20 +15,6 @@
1515
*/
1616
package org.apache.ibatis.session;
1717

18-
import java.util.Arrays;
19-
import java.util.Collection;
20-
import java.util.HashMap;
21-
import java.util.HashSet;
22-
import java.util.Iterator;
23-
import java.util.LinkedList;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Properties;
27-
import java.util.Set;
28-
import java.util.concurrent.ConcurrentHashMap;
29-
import java.util.concurrent.locks.ReentrantLock;
30-
import java.util.function.BiFunction;
31-
3218
import org.apache.ibatis.binding.MapperRegistry;
3319
import org.apache.ibatis.builder.CacheRefResolver;
3420
import org.apache.ibatis.builder.IncompleteElementException;
@@ -97,6 +83,20 @@
9783
import org.apache.ibatis.type.TypeHandler;
9884
import org.apache.ibatis.type.TypeHandlerRegistry;
9985

86+
import java.util.Arrays;
87+
import java.util.Collection;
88+
import java.util.HashMap;
89+
import java.util.HashSet;
90+
import java.util.Iterator;
91+
import java.util.LinkedList;
92+
import java.util.List;
93+
import java.util.Map;
94+
import java.util.Properties;
95+
import java.util.Set;
96+
import java.util.concurrent.ConcurrentHashMap;
97+
import java.util.concurrent.locks.ReentrantLock;
98+
import java.util.function.BiFunction;
99+
100100
/**
101101
* @author Clinton Begin
102102
*/
@@ -1113,6 +1113,7 @@ protected static class StrictMap<V> extends ConcurrentHashMap<String, V> {
11131113
private static final long serialVersionUID = -4950446264854982944L;
11141114
private final String name;
11151115
private BiFunction<V, V, String> conflictMessageProducer;
1116+
private static final Object AMBIGUITY_INSTANCE = new Object();
11161117

11171118
public StrictMap(String name, int initialCapacity, float loadFactor) {
11181119
super(initialCapacity, loadFactor);
@@ -1162,7 +1163,7 @@ public V put(String key, V value) {
11621163
if (super.get(shortKey) == null) {
11631164
super.put(shortKey, value);
11641165
} else {
1165-
super.put(shortKey, (V) new Ambiguity(shortKey));
1166+
super.put(shortKey, (V) AMBIGUITY_INSTANCE);
11661167
}
11671168
}
11681169
return super.put(key, value);
@@ -1183,25 +1184,13 @@ public V get(Object key) {
11831184
if (value == null) {
11841185
throw new IllegalArgumentException(name + " does not contain value for " + key);
11851186
}
1186-
if (value instanceof Ambiguity) {
1187-
throw new IllegalArgumentException(((Ambiguity) value).getSubject() + " is ambiguous in " + name
1187+
if (AMBIGUITY_INSTANCE == value) {
1188+
throw new IllegalArgumentException(key + " is ambiguous in " + name
11881189
+ " (try using the full name including the namespace, or rename one of the entries)");
11891190
}
11901191
return value;
11911192
}
11921193

1193-
protected static class Ambiguity {
1194-
private final String subject;
1195-
1196-
public Ambiguity(String subject) {
1197-
this.subject = subject;
1198-
}
1199-
1200-
public String getSubject() {
1201-
return subject;
1202-
}
1203-
}
1204-
12051194
private String getShortName(String key) {
12061195
final String[] keyParts = key.split("\\.");
12071196
return keyParts[keyParts.length - 1];

src/test/java/org/apache/ibatis/submitted/global_variables_defaults/ConfigurationTest.java

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,12 +15,11 @@
1515
*/
1616
package org.apache.ibatis.submitted.global_variables_defaults;
1717

18-
import java.io.IOException;
19-
import java.io.Reader;
20-
import java.util.Properties;
21-
18+
import org.apache.ibatis.builder.StaticSqlSource;
2219
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
2320
import org.apache.ibatis.io.Resources;
21+
import org.apache.ibatis.mapping.MappedStatement;
22+
import org.apache.ibatis.mapping.SqlCommandType;
2423
import org.apache.ibatis.parsing.PropertyParser;
2524
import org.apache.ibatis.session.Configuration;
2625
import org.apache.ibatis.session.SqlSessionFactory;
@@ -29,6 +28,10 @@
2928
import org.assertj.core.api.Assertions;
3029
import org.junit.jupiter.api.Test;
3130

31+
import java.io.IOException;
32+
import java.io.Reader;
33+
import java.util.Properties;
34+
3235
class ConfigurationTest {
3336

3437
@Test
@@ -79,4 +82,27 @@ void applyPropertyValueOnXmlConfiguration() throws IOException {
7982

8083
}
8184

85+
@Test
86+
void testAmbiguityCache() {
87+
Configuration configuration = new Configuration();
88+
89+
configuration.addMappedStatement(
90+
new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.selectById",
91+
new StaticSqlSource(configuration, "select * from test where id = 1"), SqlCommandType.SELECT).build());
92+
configuration
93+
.addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper1.test",
94+
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build());
95+
configuration
96+
.addMappedStatement(new MappedStatement.Builder(configuration, "org.apache.ibatis.submitted.DemoMapper2.test",
97+
new StaticSqlSource(configuration, "select * from test"), SqlCommandType.SELECT).build());
98+
99+
Assertions.assertThat(configuration.getMappedStatement("selectById")).isNotNull();
100+
Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper1.test")).isNotNull();
101+
Assertions.assertThat(configuration.getMappedStatement("org.apache.ibatis.submitted.DemoMapper2.test")).isNotNull();
102+
103+
Assertions.assertThatThrownBy(() -> configuration.getMappedStatement("test"))
104+
.isInstanceOf(IllegalArgumentException.class).hasMessage(
105+
"test is ambiguous in Mapped Statements collection (try using the full name including the namespace, or rename one of the entries)");
106+
}
107+
82108
}

0 commit comments

Comments
 (0)