Skip to content

Commit 671453a

Browse files
committed
Register driver to driver manager
1 parent a0b2fc6 commit 671453a

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

src/main/java/org/apache/ibatis/migration/JdbcConnectionProvider.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,62 @@
1616
package org.apache.ibatis.migration;
1717

1818
import java.sql.Connection;
19+
import java.sql.Driver;
1920
import java.sql.DriverManager;
2021
import java.sql.SQLException;
22+
import java.util.Enumeration;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import org.apache.ibatis.migration.driver.DriverShim;
2127

2228
public class JdbcConnectionProvider implements ConnectionProvider {
29+
private static final Map<String, Driver> registeredDrivers = registeredDrivers();
30+
2331
private final String url;
2432
private final String username;
2533
private final String password;
2634

27-
public JdbcConnectionProvider(String driver, String url, String username, String password) throws Exception {
35+
public JdbcConnectionProvider(String driver, String url, String username, String password) {
2836
this(null, driver, url, username, password);
2937
}
3038

31-
public JdbcConnectionProvider(ClassLoader classLoader, String driver, String url, String username, String password)
32-
throws Exception {
39+
public JdbcConnectionProvider(ClassLoader classLoader, String driver, String url, String username, String password) {
3340
this.url = url;
3441
this.username = username;
3542
this.password = password;
36-
37-
loadDriver(classLoader, driver);
43+
registerDriver(classLoader, driver);
3844
}
3945

4046
@Override
4147
public Connection getConnection() throws SQLException {
4248
return DriverManager.getConnection(url, username, password);
4349
}
4450

45-
private static void loadDriver(ClassLoader classLoader, String driver) throws ClassNotFoundException {
46-
if (classLoader != null) {
47-
Class.forName(driver, true, classLoader);
48-
} else {
49-
Class.forName(driver);
51+
private void registerDriver(ClassLoader classLoader, String driver) {
52+
registeredDrivers.computeIfAbsent(driver, (d) -> createDriverClass(classLoader, d));
53+
}
54+
55+
private Driver createDriverClass(ClassLoader classLoader, String driver) {
56+
try {
57+
final Class<?> driverClass = classLoader == null ? Class.forName(driver)
58+
: Class.forName(driver, true, classLoader);
59+
final Driver driverInstance = (Driver) driverClass.getDeclaredConstructor().newInstance();
60+
final DriverShim driverShim = new DriverShim(driverInstance);
61+
DriverManager.registerDriver(driverShim);
62+
return driverShim;
63+
} catch (final Exception e) {
64+
throw new IllegalStateException("Failed to register driver " + driver, e);
65+
}
66+
}
67+
68+
private static Map<String, Driver> registeredDrivers() {
69+
final Map<String, Driver> registeredDrivers = new HashMap<>();
70+
final Enumeration<Driver> drivers = DriverManager.getDrivers();
71+
while (drivers.hasMoreElements()) {
72+
final Driver driver = drivers.nextElement();
73+
registeredDrivers.put(driver.getClass().getName(), driver);
5074
}
75+
return registeredDrivers;
5176
}
5277
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2010-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.migration.driver;
17+
18+
import java.sql.Connection;
19+
import java.sql.Driver;
20+
import java.sql.DriverPropertyInfo;
21+
import java.sql.SQLException;
22+
import java.sql.SQLFeatureNotSupportedException;
23+
import java.util.Properties;
24+
import java.util.logging.Logger;
25+
26+
public class DriverShim implements Driver {
27+
private final Driver delegate;
28+
29+
public DriverShim(Driver delegate) {
30+
this.delegate = delegate;
31+
}
32+
33+
@Override
34+
public Connection connect(String url, Properties info) throws SQLException {
35+
return delegate.connect(url, info);
36+
}
37+
38+
@Override
39+
public boolean acceptsURL(String url) throws SQLException {
40+
return delegate.acceptsURL(url);
41+
}
42+
43+
@Override
44+
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
45+
return delegate.getPropertyInfo(url, info);
46+
}
47+
48+
@Override
49+
public int getMajorVersion() {
50+
return delegate.getMajorVersion();
51+
}
52+
53+
@Override
54+
public int getMinorVersion() {
55+
return delegate.getMinorVersion();
56+
}
57+
58+
@Override
59+
public boolean jdbcCompliant() {
60+
return delegate.jdbcCompliant();
61+
}
62+
63+
@Override
64+
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
65+
return delegate.getParentLogger();
66+
}
67+
}

0 commit comments

Comments
 (0)