1
1
/*
2
- * Copyright 2009-2023 the original author or authors.
2
+ * Copyright 2009-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import java .util .Properties ;
27
27
import java .util .concurrent .ConcurrentHashMap ;
28
28
import java .util .concurrent .Executors ;
29
- import java .util .concurrent .locks .ReentrantReadWriteLock ;
30
29
import java .util .logging .Logger ;
31
30
32
31
import javax .sql .DataSource ;
33
32
34
33
import org .apache .ibatis .io .Resources ;
34
+ import org .apache .ibatis .util .MapUtil ;
35
35
36
36
/**
37
37
* @author Clinton Begin
@@ -42,7 +42,6 @@ public class UnpooledDataSource implements DataSource {
42
42
private ClassLoader driverClassLoader ;
43
43
private Properties driverProperties ;
44
44
private static final Map <String , Driver > registeredDrivers = new ConcurrentHashMap <>();
45
- private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock ();
46
45
47
46
private String driver ;
48
47
private String url ;
@@ -140,23 +139,11 @@ public void setDriverProperties(Properties driverProperties) {
140
139
}
141
140
142
141
public String getDriver () {
143
- readWriteLock .readLock ().lock ();
144
- try {
145
- return driver ;
146
- } finally {
147
- readWriteLock .readLock ().unlock ();
148
- }
149
-
142
+ return driver ;
150
143
}
151
144
152
145
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 ;
160
147
}
161
148
162
149
public String getUrl () {
@@ -245,25 +232,24 @@ private Connection doGetConnection(Properties properties) throws SQLException {
245
232
}
246
233
247
234
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 );
256
249
}
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 ());
267
253
}
268
254
}
269
255
0 commit comments