Skip to content

Commit 43b0f8c

Browse files
authored
Merge pull request #3017 from m1ngyuan/patch-1
Remove `synchronized` methods/blocks from the codebase
2 parents 970bde0 + 0949fc7 commit 43b0f8c

File tree

14 files changed

+416
-270
lines changed

14 files changed

+416
-270
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 2 additions & 18 deletions
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-2024 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.
@@ -27,7 +27,6 @@
2727
import java.util.Arrays;
2828
import java.util.Collection;
2929
import java.util.HashMap;
30-
import java.util.Iterator;
3130
import java.util.List;
3231
import java.util.Map;
3332
import java.util.Optional;
@@ -135,29 +134,14 @@ public void parse() {
135134
}
136135
}
137136
}
138-
parsePendingMethods();
137+
configuration.parsePendingMethods(false);
139138
}
140139

141140
private static boolean canHaveStatement(Method method) {
142141
// issue #237
143142
return !method.isBridge() && !method.isDefault();
144143
}
145144

146-
private void parsePendingMethods() {
147-
Collection<MethodResolver> incompleteMethods = configuration.getIncompleteMethods();
148-
synchronized (incompleteMethods) {
149-
Iterator<MethodResolver> iter = incompleteMethods.iterator();
150-
while (iter.hasNext()) {
151-
try {
152-
iter.next().resolve();
153-
iter.remove();
154-
} catch (IncompleteElementException e) {
155-
// This method is still missing a resource
156-
}
157-
}
158-
}
159-
}
160-
161145
private void loadXmlResource() {
162146
// Spring may not know the real resource name so we check a flag
163147
// to prevent loading again a resource twice

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 4 additions & 51 deletions
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-2024 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.
@@ -19,10 +19,8 @@
1919
import java.io.Reader;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22-
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.HashMap;
25-
import java.util.Iterator;
2624
import java.util.List;
2725
import java.util.Map;
2826
import java.util.Properties;
@@ -101,9 +99,9 @@ public void parse() {
10199
configuration.addLoadedResource(resource);
102100
bindMapperForNamespace();
103101
}
104-
parsePendingResultMaps();
105-
parsePendingCacheRefs();
106-
parsePendingStatements();
102+
configuration.parsePendingResultMaps(false);
103+
configuration.parsePendingCacheRefs(false);
104+
configuration.parsePendingStatements(false);
107105
}
108106

109107
public XNode getSqlFragment(String refid) {
@@ -147,51 +145,6 @@ private void buildStatementFromContext(List<XNode> list, String requiredDatabase
147145
}
148146
}
149147

150-
private void parsePendingResultMaps() {
151-
Collection<ResultMapResolver> incompleteResultMaps = configuration.getIncompleteResultMaps();
152-
synchronized (incompleteResultMaps) {
153-
Iterator<ResultMapResolver> iter = incompleteResultMaps.iterator();
154-
while (iter.hasNext()) {
155-
try {
156-
iter.next().resolve();
157-
iter.remove();
158-
} catch (IncompleteElementException e) {
159-
// ResultMap is still missing a resource...
160-
}
161-
}
162-
}
163-
}
164-
165-
private void parsePendingCacheRefs() {
166-
Collection<CacheRefResolver> incompleteCacheRefs = configuration.getIncompleteCacheRefs();
167-
synchronized (incompleteCacheRefs) {
168-
Iterator<CacheRefResolver> iter = incompleteCacheRefs.iterator();
169-
while (iter.hasNext()) {
170-
try {
171-
iter.next().resolveCacheRef();
172-
iter.remove();
173-
} catch (IncompleteElementException e) {
174-
// Cache ref is still missing a resource...
175-
}
176-
}
177-
}
178-
}
179-
180-
private void parsePendingStatements() {
181-
Collection<XMLStatementBuilder> incompleteStatements = configuration.getIncompleteStatements();
182-
synchronized (incompleteStatements) {
183-
Iterator<XMLStatementBuilder> iter = incompleteStatements.iterator();
184-
while (iter.hasNext()) {
185-
try {
186-
iter.next().parseStatementNode();
187-
iter.remove();
188-
} catch (IncompleteElementException e) {
189-
// Statement is still missing a resource...
190-
}
191-
}
192-
}
193-
}
194-
195148
private void cacheRefElement(XNode context) {
196149
if (context != null) {
197150
configuration.addCacheRef(builderAssistant.getCurrentNamespace(), context.getStringAttribute("namespace"));

src/main/java/org/apache/ibatis/cache/decorators/SoftCache.java

Lines changed: 11 additions & 3 deletions
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-2024 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.
@@ -19,6 +19,7 @@
1919
import java.lang.ref.SoftReference;
2020
import java.util.Deque;
2121
import java.util.LinkedList;
22+
import java.util.concurrent.locks.ReentrantLock;
2223

2324
import org.apache.ibatis.cache.Cache;
2425

@@ -34,6 +35,7 @@ public class SoftCache implements Cache {
3435
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
3536
private final Cache delegate;
3637
private int numberOfHardLinks;
38+
private final ReentrantLock lock = new ReentrantLock();
3739

3840
public SoftCache(Cache delegate) {
3941
this.delegate = delegate;
@@ -74,11 +76,14 @@ public Object getObject(Object key) {
7476
delegate.removeObject(key);
7577
} else {
7678
// See #586 (and #335) modifications need more than a read lock
77-
synchronized (hardLinksToAvoidGarbageCollection) {
79+
lock.lock();
80+
try {
7881
hardLinksToAvoidGarbageCollection.addFirst(result);
7982
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
8083
hardLinksToAvoidGarbageCollection.removeLast();
8184
}
85+
} finally {
86+
lock.unlock();
8287
}
8388
}
8489
}
@@ -95,8 +100,11 @@ public Object removeObject(Object key) {
95100

96101
@Override
97102
public void clear() {
98-
synchronized (hardLinksToAvoidGarbageCollection) {
103+
lock.lock();
104+
try {
99105
hardLinksToAvoidGarbageCollection.clear();
106+
} finally {
107+
lock.unlock();
100108
}
101109
removeGarbageCollectedItems();
102110
delegate.clear();

src/main/java/org/apache/ibatis/cache/decorators/SynchronizedCache.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2022 the original author or authors.
2+
* Copyright 2009-2024 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,13 +15,16 @@
1515
*/
1616
package org.apache.ibatis.cache.decorators;
1717

18+
import java.util.concurrent.locks.ReentrantLock;
19+
1820
import org.apache.ibatis.cache.Cache;
1921

2022
/**
2123
* @author Clinton Begin
2224
*/
2325
public class SynchronizedCache implements Cache {
2426

27+
private final ReentrantLock lock = new ReentrantLock();
2528
private final Cache delegate;
2629

2730
public SynchronizedCache(Cache delegate) {
@@ -34,28 +37,53 @@ public String getId() {
3437
}
3538

3639
@Override
37-
public synchronized int getSize() {
38-
return delegate.getSize();
40+
public int getSize() {
41+
lock.lock();
42+
try {
43+
return delegate.getSize();
44+
} finally {
45+
lock.unlock();
46+
}
3947
}
4048

4149
@Override
42-
public synchronized void putObject(Object key, Object object) {
43-
delegate.putObject(key, object);
50+
public void putObject(Object key, Object object) {
51+
lock.lock();
52+
try {
53+
delegate.putObject(key, object);
54+
} finally {
55+
lock.unlock();
56+
}
4457
}
4558

4659
@Override
47-
public synchronized Object getObject(Object key) {
48-
return delegate.getObject(key);
60+
public Object getObject(Object key) {
61+
lock.lock();
62+
try {
63+
return delegate.getObject(key);
64+
} finally {
65+
lock.unlock();
66+
}
4967
}
5068

5169
@Override
52-
public synchronized Object removeObject(Object key) {
53-
return delegate.removeObject(key);
70+
public Object removeObject(Object key) {
71+
lock.lock();
72+
try {
73+
return delegate.removeObject(key);
74+
} finally {
75+
lock.unlock();
76+
}
5477
}
5578

5679
@Override
57-
public synchronized void clear() {
58-
delegate.clear();
80+
public void clear() {
81+
lock.lock();
82+
try {
83+
delegate.clear();
84+
} finally {
85+
lock.unlock();
86+
}
5987
}
6088

6189
@Override

src/main/java/org/apache/ibatis/cache/decorators/WeakCache.java

Lines changed: 11 additions & 3 deletions
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-2024 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.
@@ -19,6 +19,7 @@
1919
import java.lang.ref.WeakReference;
2020
import java.util.Deque;
2121
import java.util.LinkedList;
22+
import java.util.concurrent.locks.ReentrantLock;
2223

2324
import org.apache.ibatis.cache.Cache;
2425

@@ -34,6 +35,7 @@ public class WeakCache implements Cache {
3435
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
3536
private final Cache delegate;
3637
private int numberOfHardLinks;
38+
private final ReentrantLock lock = new ReentrantLock();
3739

3840
public WeakCache(Cache delegate) {
3941
this.delegate = delegate;
@@ -73,11 +75,14 @@ public Object getObject(Object key) {
7375
if (result == null) {
7476
delegate.removeObject(key);
7577
} else {
76-
synchronized (hardLinksToAvoidGarbageCollection) {
78+
lock.lock();
79+
try {
7780
hardLinksToAvoidGarbageCollection.addFirst(result);
7881
if (hardLinksToAvoidGarbageCollection.size() > numberOfHardLinks) {
7982
hardLinksToAvoidGarbageCollection.removeLast();
8083
}
84+
} finally {
85+
lock.unlock();
8186
}
8287
}
8388
}
@@ -94,8 +99,11 @@ public Object removeObject(Object key) {
9499

95100
@Override
96101
public void clear() {
97-
synchronized (hardLinksToAvoidGarbageCollection) {
102+
lock.lock();
103+
try {
98104
hardLinksToAvoidGarbageCollection.clear();
105+
} finally {
106+
lock.unlock();
99107
}
100108
removeGarbageCollectedItems();
101109
delegate.clear();

0 commit comments

Comments
 (0)