Skip to content

Commit 50060a6

Browse files
committed
Use the java.util.concurrent.locks.ReentrantLock directly
1 parent 4d70435 commit 50060a6

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

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

Lines changed: 3 additions & 4 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,9 +19,9 @@
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;
24-
import org.apache.ibatis.util.LockKit;
2525

2626
/**
2727
* Soft Reference cache decorator.
@@ -35,6 +35,7 @@ public class SoftCache implements Cache {
3535
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
3636
private final Cache delegate;
3737
private int numberOfHardLinks;
38+
private final ReentrantLock lock = new ReentrantLock();
3839

3940
public SoftCache(Cache delegate) {
4041
this.delegate = delegate;
@@ -75,7 +76,6 @@ public Object getObject(Object key) {
7576
delegate.removeObject(key);
7677
} else {
7778
// See #586 (and #335) modifications need more than a read lock
78-
LockKit.ReentrantLock lock = LockKit.obtainLock(hardLinksToAvoidGarbageCollection);
7979
lock.lock();
8080
try {
8181
hardLinksToAvoidGarbageCollection.addFirst(result);
@@ -100,7 +100,6 @@ public Object removeObject(Object key) {
100100

101101
@Override
102102
public void clear() {
103-
LockKit.ReentrantLock lock = LockKit.obtainLock(hardLinksToAvoidGarbageCollection);
104103
lock.lock();
105104
try {
106105
hardLinksToAvoidGarbageCollection.clear();

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

Lines changed: 3 additions & 4 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,9 +19,9 @@
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;
24-
import org.apache.ibatis.util.LockKit;
2525

2626
/**
2727
* Weak Reference cache decorator.
@@ -35,6 +35,7 @@ public class WeakCache implements Cache {
3535
private final ReferenceQueue<Object> queueOfGarbageCollectedEntries;
3636
private final Cache delegate;
3737
private int numberOfHardLinks;
38+
private final ReentrantLock lock = new ReentrantLock();
3839

3940
public WeakCache(Cache delegate) {
4041
this.delegate = delegate;
@@ -74,7 +75,6 @@ public Object getObject(Object key) {
7475
if (result == null) {
7576
delegate.removeObject(key);
7677
} else {
77-
LockKit.ReentrantLock lock = LockKit.obtainLock(hardLinksToAvoidGarbageCollection);
7878
lock.lock();
7979
try {
8080
hardLinksToAvoidGarbageCollection.addFirst(result);
@@ -99,7 +99,6 @@ public Object removeObject(Object key) {
9999

100100
@Override
101101
public void clear() {
102-
LockKit.ReentrantLock lock = LockKit.obtainLock(hardLinksToAvoidGarbageCollection);
103102
lock.lock();
104103
try {
105104
hardLinksToAvoidGarbageCollection.clear();

src/main/java/org/apache/ibatis/datasource/pooled/PoolState.java

Lines changed: 30 additions & 27 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.
@@ -17,15 +17,19 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20-
21-
import org.apache.ibatis.util.LockKit;
20+
import java.util.concurrent.locks.ReentrantLock;
2221

2322
/**
2423
* @author Clinton Begin
2524
*/
2625
public class PoolState {
2726

28-
private final LockKit.ReentrantLock reentrantLock;
27+
// This lock does not guarantee consistency.
28+
// Field values can be modified in PooledDataSource
29+
// after the instance is returned from
30+
// PooledDataSource#getPoolState().
31+
// A possible fix is to create and return a 'snapshot'.
32+
private final ReentrantLock lock = new ReentrantLock();
2933

3034
protected PooledDataSource dataSource;
3135

@@ -42,103 +46,102 @@ public class PoolState {
4246

4347
public PoolState(PooledDataSource dataSource) {
4448
this.dataSource = dataSource;
45-
this.reentrantLock = LockKit.obtainLock(dataSource);
4649
}
4750

4851
public long getRequestCount() {
49-
reentrantLock.lock();
52+
lock.lock();
5053
try {
5154
return requestCount;
5255
} finally {
53-
reentrantLock.unlock();
56+
lock.unlock();
5457
}
5558
}
5659

5760
public long getAverageRequestTime() {
58-
reentrantLock.lock();
61+
lock.lock();
5962
try {
6063
return requestCount == 0 ? 0 : accumulatedRequestTime / requestCount;
6164
} finally {
62-
reentrantLock.unlock();
65+
lock.unlock();
6366
}
6467
}
6568

6669
public long getAverageWaitTime() {
67-
reentrantLock.lock();
70+
lock.lock();
6871
try {
6972
return hadToWaitCount == 0 ? 0 : accumulatedWaitTime / hadToWaitCount;
7073
} finally {
71-
reentrantLock.unlock();
74+
lock.unlock();
7275
}
7376
}
7477

7578
public long getHadToWaitCount() {
76-
reentrantLock.lock();
79+
lock.lock();
7780
try {
7881
return hadToWaitCount;
7982
} finally {
80-
reentrantLock.unlock();
83+
lock.unlock();
8184
}
8285
}
8386

8487
public long getBadConnectionCount() {
85-
reentrantLock.lock();
88+
lock.lock();
8689
try {
8790
return badConnectionCount;
8891
} finally {
89-
reentrantLock.unlock();
92+
lock.unlock();
9093
}
9194
}
9295

9396
public long getClaimedOverdueConnectionCount() {
94-
reentrantLock.lock();
97+
lock.lock();
9598
try {
9699
return claimedOverdueConnectionCount;
97100
} finally {
98-
reentrantLock.unlock();
101+
lock.unlock();
99102
}
100103
}
101104

102105
public long getAverageOverdueCheckoutTime() {
103-
reentrantLock.lock();
106+
lock.lock();
104107
try {
105108
return claimedOverdueConnectionCount == 0 ? 0
106109
: accumulatedCheckoutTimeOfOverdueConnections / claimedOverdueConnectionCount;
107110
} finally {
108-
reentrantLock.unlock();
111+
lock.unlock();
109112
}
110113
}
111114

112115
public long getAverageCheckoutTime() {
113-
reentrantLock.lock();
116+
lock.lock();
114117
try {
115118
return requestCount == 0 ? 0 : accumulatedCheckoutTime / requestCount;
116119
} finally {
117-
reentrantLock.unlock();
120+
lock.unlock();
118121
}
119122
}
120123

121124
public int getIdleConnectionCount() {
122-
reentrantLock.lock();
125+
lock.lock();
123126
try {
124127
return idleConnections.size();
125128
} finally {
126-
reentrantLock.unlock();
129+
lock.unlock();
127130
}
128131
}
129132

130133
public int getActiveConnectionCount() {
131-
reentrantLock.lock();
134+
lock.lock();
132135
try {
133136
return activeConnections.size();
134137
} finally {
135-
reentrantLock.unlock();
138+
lock.unlock();
136139
}
137140
}
138141

139142
@Override
140143
public String toString() {
141-
reentrantLock.lock();
144+
lock.lock();
142145
try {
143146
StringBuilder builder = new StringBuilder();
144147
builder.append("\n===CONFIGURATION==============================================");
@@ -168,7 +171,7 @@ public String toString() {
168171
builder.append("\n===============================================================");
169172
return builder.toString();
170173
} finally {
171-
reentrantLock.unlock();
174+
lock.unlock();
172175
}
173176
}
174177

src/main/java/org/apache/ibatis/executor/loader/AbstractEnhancedDeserializationProxy.java

Lines changed: 5 additions & 6 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,13 +19,13 @@
1919
import java.util.List;
2020
import java.util.Locale;
2121
import java.util.Map;
22+
import java.util.concurrent.locks.ReentrantLock;
2223

2324
import org.apache.ibatis.executor.ExecutorException;
2425
import org.apache.ibatis.reflection.ExceptionUtil;
2526
import org.apache.ibatis.reflection.factory.ObjectFactory;
2627
import org.apache.ibatis.reflection.property.PropertyCopier;
2728
import org.apache.ibatis.reflection.property.PropertyNamer;
28-
import org.apache.ibatis.util.LockKit;
2929

3030
/**
3131
* @author Clinton Begin
@@ -39,7 +39,7 @@ public abstract class AbstractEnhancedDeserializationProxy {
3939
private final ObjectFactory objectFactory;
4040
private final List<Class<?>> constructorArgTypes;
4141
private final List<Object> constructorArgs;
42-
private final LockKit.ReentrantLock reentrantLock;
42+
private final ReentrantLock lock = new ReentrantLock();
4343
private boolean reloadingProperty;
4444

4545
protected AbstractEnhancedDeserializationProxy(Class<?> type,
@@ -50,7 +50,6 @@ protected AbstractEnhancedDeserializationProxy(Class<?> type,
5050
this.objectFactory = objectFactory;
5151
this.constructorArgTypes = constructorArgTypes;
5252
this.constructorArgs = constructorArgs;
53-
this.reentrantLock = new LockKit.ReentrantLock();
5453
this.reloadingProperty = false;
5554
}
5655

@@ -69,7 +68,7 @@ public final Object invoke(Object enhanced, Method method, Object[] args) throws
6968
return this.newSerialStateHolder(original, unloadedProperties, objectFactory, constructorArgTypes,
7069
constructorArgs);
7170
}
72-
reentrantLock.lock();
71+
lock.lock();
7372
try {
7473
if (!FINALIZE_METHOD.equals(methodName) && PropertyNamer.isProperty(methodName) && !reloadingProperty) {
7574
final String property = PropertyNamer.methodToProperty(methodName);
@@ -96,7 +95,7 @@ public final Object invoke(Object enhanced, Method method, Object[] args) throws
9695

9796
return enhanced;
9897
} finally {
99-
reentrantLock.unlock();
98+
lock.unlock();
10099
}
101100
} catch (Throwable t) {
102101
throw ExceptionUtil.unwrapThrowable(t);

src/main/java/org/apache/ibatis/executor/loader/cglib/CglibProxyFactory.java

Lines changed: 3 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.util.List;
2020
import java.util.Map;
2121
import java.util.Set;
22+
import java.util.concurrent.locks.ReentrantLock;
2223

2324
import net.sf.cglib.proxy.Callback;
2425
import net.sf.cglib.proxy.Enhancer;
@@ -38,7 +39,6 @@
3839
import org.apache.ibatis.reflection.property.PropertyCopier;
3940
import org.apache.ibatis.reflection.property.PropertyNamer;
4041
import org.apache.ibatis.session.Configuration;
41-
import org.apache.ibatis.util.LockKit;
4242

4343
/**
4444
* @author Clinton Begin
@@ -110,6 +110,7 @@ private static class EnhancedResultObjectProxyImpl implements MethodInterceptor
110110
private final ObjectFactory objectFactory;
111111
private final List<Class<?>> constructorArgTypes;
112112
private final List<Object> constructorArgs;
113+
private final ReentrantLock lock = new ReentrantLock();
113114

114115
private EnhancedResultObjectProxyImpl(Class<?> type, ResultLoaderMap lazyLoader, Configuration configuration,
115116
ObjectFactory objectFactory, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
@@ -135,7 +136,6 @@ public static Object createProxy(Object target, ResultLoaderMap lazyLoader, Conf
135136
@Override
136137
public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
137138
final String methodName = method.getName();
138-
LockKit.ReentrantLock lock = LockKit.obtainLock(lazyLoader);
139139
lock.lock();
140140
try {
141141
if (WRITE_REPLACE_METHOD.equals(methodName)) {

0 commit comments

Comments
 (0)