Skip to content

Commit 148dc95

Browse files
committed
Fix regressions in SimpleThreadScope and SimpleTransactionScope
PR gh-25038 introduced regressions in SimpleThreadScope and SimpleTransactionScope in Spring Framework 5.2.7. Specifically, if a thread-scoped or transaction-scoped bean has a dependency on another thread-scoped or transaction-scoped bean, respectively, a ConcurrentModificationException will be thrown on Java 11 or higher. The reason is that Java 11 introduced a check for concurrent modification in java.util.HashMap's computeIfAbsent() implementation, and such a modification can occur when a thread-scoped bean is being created in order to satisfy a dependency of another thread-scoped bean that is currently being created. This commit fixes these regressions by switching from HashMap to ConcurrentHashMap for the instance maps in SimpleThreadScope and SimpleTransactionScope. Closes gh-25618
1 parent d939016 commit 148dc95

File tree

4 files changed

+16
-46
lines changed

4 files changed

+16
-46
lines changed

spring-context/src/main/java/org/springframework/context/support/SimpleThreadScope.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -16,8 +16,8 @@
1616

1717
package org.springframework.context.support;
1818

19-
import java.util.HashMap;
2019
import java.util.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
@@ -59,7 +59,7 @@ public class SimpleThreadScope implements Scope {
5959
new NamedThreadLocal<Map<String, Object>>("SimpleThreadScope") {
6060
@Override
6161
protected Map<String, Object> initialValue() {
62-
return new HashMap<>();
62+
return new ConcurrentHashMap<>();
6363
}
6464
};
6565

spring-context/src/test/java/org/springframework/context/support/SimpleThreadScopeTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -30,14 +30,14 @@
3030
* @author Arjen Poutsma
3131
* @author Sam Brannen
3232
*/
33-
public class SimpleThreadScopeTests {
33+
class SimpleThreadScopeTests {
3434

3535
private final ApplicationContext applicationContext =
3636
new ClassPathXmlApplicationContext("simpleThreadScopeTests.xml", getClass());
3737

3838

3939
@Test
40-
public void getFromScope() throws Exception {
40+
void getFromScope() throws Exception {
4141
String name = "threadScopedObject";
4242
TestBean bean = this.applicationContext.getBean(name, TestBean.class);
4343
assertThat(bean).isNotNull();
@@ -47,7 +47,7 @@ public void getFromScope() throws Exception {
4747
}
4848

4949
@Test
50-
public void getMultipleInstances() throws Exception {
50+
void getMultipleInstances() throws Exception {
5151
// Arrange
5252
TestBean[] beans = new TestBean[2];
5353
Thread thread1 = new Thread(() -> beans[0] = applicationContext.getBean("threadScopedObject", TestBean.class));
Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://www.springframework.org/schema/beans
5-
https://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
2+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
64

75
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
86
<property name="scopes">
97
<map>
108
<entry key="thread">
11-
<bean class="org.springframework.context.support.SimpleThreadScope"/>
9+
<bean class="org.springframework.context.support.SimpleThreadScope" />
1210
</entry>
1311
</map>
1412
</property>
1513
</bean>
1614

17-
<bean id="threadScopedObject" class="org.springframework.beans.testfixture.beans.TestBean" scope="thread"/>
18-
19-
<!--
20-
<bean id="requestScopedDisposableObject" class="org.springframework.beans.testfixture.beans.DerivedTestBean" scope="request"/>
21-
22-
<bean id="requestScopedFactoryBean" class="org.springframework.beans.testfixture.beans.factory.DummyFactory" scope="request"/>
23-
24-
<bean id="requestScopedObjectCircle1" class="org.springframework.beans.testfixture.beans.TestBean" scope="request">
25-
<property name="spouse" ref="requestScopedObjectCircle2"/>
15+
<bean id="threadScopedObject" class="org.springframework.beans.testfixture.beans.TestBean" scope="thread">
16+
<property name="spouse" ref="threadScopedObject2" />
2617
</bean>
2718

28-
<bean id="requestScopedObjectCircle2" class="org.springframework.beans.testfixture.beans.TestBean" scope="request">
29-
<property name="spouse" ref="requestScopedObjectCircle1"/>
30-
</bean>
31-
32-
<bean id="requestScopedOuterBean" class="org.springframework.beans.testfixture.beans.DerivedTestBean" scope="request">
33-
<property name="name" value="outer"/>
34-
<property name="spouse">
35-
<bean class="org.springframework.beans.testfixture.beans.DerivedTestBean">
36-
<property name="name" value="inner"/>
37-
</bean>
38-
</property>
39-
</bean>
40-
41-
<bean id="singletonOuterBean" class="org.springframework.beans.testfixture.beans.DerivedTestBean" lazy-init="true">
42-
<property name="name" value="outer"/>
43-
<property name="spouse">
44-
<bean class="org.springframework.beans.testfixture.beans.DerivedTestBean" scope="request">
45-
<property name="name" value="inner"/>
46-
</bean>
47-
</property>
48-
</bean>
19+
<bean id="threadScopedObject2" class="org.springframework.beans.testfixture.beans.TestBean" scope="thread" />
4920

50-
-->
5121
</beans>

spring-tx/src/main/java/org/springframework/transaction/support/SimpleTransactionScope.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.transaction.support;
1818

19-
import java.util.HashMap;
2019
import java.util.LinkedHashMap;
2120
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
2222

2323
import org.springframework.beans.factory.ObjectFactory;
2424
import org.springframework.beans.factory.config.Scope;
@@ -92,7 +92,7 @@ public String getConversationId() {
9292
*/
9393
static class ScopedObjectsHolder {
9494

95-
final Map<String, Object> scopedInstances = new HashMap<>();
95+
final Map<String, Object> scopedInstances = new ConcurrentHashMap<>();
9696

9797
final Map<String, Runnable> destructionCallbacks = new LinkedHashMap<>();
9898
}

0 commit comments

Comments
 (0)