Skip to content

Commit 79f33a4

Browse files
committed
Use computeIfAbsent instead of explicit lock
1 parent 69b3820 commit 79f33a4

File tree

1 file changed

+21
-35
lines changed

1 file changed

+21
-35
lines changed

src/main/java/org/apache/ibatis/datasource/unpooled/UnpooledDataSource.java

Lines changed: 21 additions & 35 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.
@@ -26,12 +26,12 @@
2626
import java.util.Properties;
2727
import java.util.concurrent.ConcurrentHashMap;
2828
import java.util.concurrent.Executors;
29-
import java.util.concurrent.locks.ReentrantReadWriteLock;
3029
import java.util.logging.Logger;
3130

3231
import javax.sql.DataSource;
3332

3433
import org.apache.ibatis.io.Resources;
34+
import org.apache.ibatis.util.MapUtil;
3535

3636
/**
3737
* @author Clinton Begin
@@ -42,7 +42,6 @@ public class UnpooledDataSource implements DataSource {
4242
private ClassLoader driverClassLoader;
4343
private Properties driverProperties;
4444
private static final Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>();
45-
private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
4645

4746
private String driver;
4847
private String url;
@@ -140,23 +139,11 @@ public void setDriverProperties(Properties driverProperties) {
140139
}
141140

142141
public String getDriver() {
143-
readWriteLock.readLock().lock();
144-
try {
145-
return driver;
146-
} finally {
147-
readWriteLock.readLock().unlock();
148-
}
149-
142+
return driver;
150143
}
151144

152145
public void setDriver(String driver) {
153-
readWriteLock.writeLock().lock();
154-
try {
155-
this.driver = driver;
156-
} finally {
157-
readWriteLock.writeLock().unlock();
158-
}
159-
146+
this.driver = driver;
160147
}
161148

162149
public String getUrl() {
@@ -245,25 +232,24 @@ private Connection doGetConnection(Properties properties) throws SQLException {
245232
}
246233

247234
private void initializeDriver() throws SQLException {
248-
if (!registeredDrivers.containsKey(driver)) {
249-
Class<?> driverType;
250-
readWriteLock.readLock().lock();
251-
try {
252-
if (driverClassLoader != null) {
253-
driverType = Class.forName(driver, true, driverClassLoader);
254-
} else {
255-
driverType = Resources.classForName(driver);
235+
try {
236+
MapUtil.computeIfAbsent(registeredDrivers, driver, x -> {
237+
Class<?> driverType;
238+
try {
239+
if (driverClassLoader != null) {
240+
driverType = Class.forName(x, true, driverClassLoader);
241+
} else {
242+
driverType = Resources.classForName(x);
243+
}
244+
Driver driverInstance = (Driver) driverType.getDeclaredConstructor().newInstance();
245+
DriverManager.registerDriver(new DriverProxy(driverInstance));
246+
return driverInstance;
247+
} catch (Exception e) {
248+
throw new RuntimeException("Error setting driver on UnpooledDataSource.", e);
256249
}
257-
// DriverManager requires the driver to be loaded via the system ClassLoader.
258-
// https://www.kfu.com/~nsayer/Java/dyn-jdbc.html
259-
Driver driverInstance = (Driver) driverType.getDeclaredConstructor().newInstance();
260-
DriverManager.registerDriver(new DriverProxy(driverInstance));
261-
registeredDrivers.put(driver, driverInstance);
262-
} catch (Exception e) {
263-
throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
264-
} finally {
265-
readWriteLock.readLock().lock();
266-
}
250+
});
251+
} catch (RuntimeException re) {
252+
throw new SQLException("Error setting driver on UnpooledDataSource.", re.getCause());
267253
}
268254
}
269255

0 commit comments

Comments
 (0)