17
17
18
18
import java .io .PrintWriter ;
19
19
import java .sql .Connection ;
20
+ import java .sql .Driver ;
20
21
import java .sql .DriverManager ;
22
+ import java .sql .DriverPropertyInfo ;
21
23
import java .sql .SQLException ;
24
+ import java .util .Enumeration ;
25
+ import java .util .Map ;
22
26
import java .util .Properties ;
27
+ import java .util .concurrent .ConcurrentHashMap ;
23
28
import java .util .logging .Logger ;
24
29
25
30
import javax .sql .DataSource ;
@@ -30,7 +35,7 @@ public class UnpooledDataSource implements DataSource {
30
35
31
36
private ClassLoader driverClassLoader ;
32
37
private Properties driverProperties ;
33
- private boolean driverInitialized ;
38
+ private static Map < String , Driver > driverCache = new ConcurrentHashMap < String , Driver >() ;
34
39
35
40
private String driver ;
36
41
private String url ;
@@ -41,7 +46,11 @@ public class UnpooledDataSource implements DataSource {
41
46
private Integer defaultTransactionIsolationLevel ;
42
47
43
48
static {
44
- DriverManager .getDrivers ();
49
+ Enumeration <Driver > drivers = DriverManager .getDrivers ();
50
+ while (drivers .hasMoreElements ()) {
51
+ Driver driver = drivers .nextElement ();
52
+ driverCache .put (driver .getClass ().getName (), driver );
53
+ }
45
54
}
46
55
47
56
public UnpooledDataSource () {
@@ -121,7 +130,6 @@ public String getDriver() {
121
130
122
131
public synchronized void setDriver (String driver ) {
123
132
this .driver = driver ;
124
- driverInitialized = false ;
125
133
}
126
134
127
135
public String getUrl () {
@@ -183,14 +191,18 @@ private Connection doGetConnection(Properties properties) throws SQLException {
183
191
}
184
192
185
193
private synchronized void initializeDriver () throws SQLException {
186
- if (!driverInitialized ) {
187
- driverInitialized = true ;
194
+ if (!driverCache . containsKey ( driver ) ) {
195
+ Class <?> driverType ;
188
196
try {
189
197
if (driverClassLoader != null ) {
190
- Class .forName (driver , true , driverClassLoader );
198
+ driverType = Class .forName (driver , true , driverClassLoader );
191
199
} else {
192
- Resources .classForName (driver );
200
+ driverType = Resources .classForName (driver );
193
201
}
202
+ // http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
203
+ Driver driverInstance = (Driver )driverType .newInstance ();
204
+ DriverManager .registerDriver (new DriverProxy (driverInstance ));
205
+ driverCache .put (driver , driverInstance );
194
206
} catch (Exception e ) {
195
207
throw new SQLException ("Error setting driver on UnpooledDataSource. Cause: " + e );
196
208
}
@@ -206,6 +218,38 @@ private void configureConnection(Connection conn) throws SQLException {
206
218
}
207
219
}
208
220
221
+ private static class DriverProxy implements Driver {
222
+ private Driver driver ;
223
+
224
+ DriverProxy (Driver d ) {
225
+ this .driver = d ;
226
+ }
227
+
228
+ public boolean acceptsURL (String u ) throws SQLException {
229
+ return this .driver .acceptsURL (u );
230
+ }
231
+
232
+ public Connection connect (String u , Properties p ) throws SQLException {
233
+ return this .driver .connect (u , p );
234
+ }
235
+
236
+ public int getMajorVersion () {
237
+ return this .driver .getMajorVersion ();
238
+ }
239
+
240
+ public int getMinorVersion () {
241
+ return this .driver .getMinorVersion ();
242
+ }
243
+
244
+ public DriverPropertyInfo [] getPropertyInfo (String u , Properties p ) throws SQLException {
245
+ return this .driver .getPropertyInfo (u , p );
246
+ }
247
+
248
+ public boolean jdbcCompliant () {
249
+ return this .driver .jdbcCompliant ();
250
+ }
251
+ }
252
+
209
253
public <T > T unwrap (Class <T > iface ) throws SQLException {
210
254
throw new SQLException (getClass ().getName () + " is not a wrapper." );
211
255
}
0 commit comments