|
16 | 16 | package org.apache.ibatis.migration; |
17 | 17 |
|
18 | 18 | import java.sql.Connection; |
| 19 | +import java.sql.Driver; |
19 | 20 | import java.sql.DriverManager; |
20 | 21 | 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; |
21 | 27 |
|
22 | 28 | public class JdbcConnectionProvider implements ConnectionProvider { |
| 29 | + private static final Map<String, Driver> registeredDrivers = registeredDrivers(); |
| 30 | + |
23 | 31 | private final String url; |
24 | 32 | private final String username; |
25 | 33 | private final String password; |
26 | 34 |
|
27 | | - public JdbcConnectionProvider(String driver, String url, String username, String password) throws Exception { |
| 35 | + public JdbcConnectionProvider(String driver, String url, String username, String password) { |
28 | 36 | this(null, driver, url, username, password); |
29 | 37 | } |
30 | 38 |
|
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) { |
33 | 40 | this.url = url; |
34 | 41 | this.username = username; |
35 | 42 | this.password = password; |
36 | | - |
37 | | - loadDriver(classLoader, driver); |
| 43 | + registerDriver(classLoader, driver); |
38 | 44 | } |
39 | 45 |
|
40 | 46 | @Override |
41 | 47 | public Connection getConnection() throws SQLException { |
42 | 48 | return DriverManager.getConnection(url, username, password); |
43 | 49 | } |
44 | 50 |
|
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); |
50 | 74 | } |
| 75 | + return registeredDrivers; |
51 | 76 | } |
52 | 77 | } |
0 commit comments