|
8 | 8 |
|
9 | 9 | import java.sql.Connection; |
10 | 10 | import java.sql.Driver; |
| 11 | +import java.sql.DriverManager; |
11 | 12 | import java.sql.SQLException; |
| 13 | +import java.util.List; |
12 | 14 | import java.util.Map; |
13 | 15 | import java.util.ServiceLoader; |
14 | 16 | import java.util.concurrent.ConcurrentHashMap; |
@@ -61,19 +63,28 @@ public Connection getConnection(final String jdbcUrl, final String userName, fin |
61 | 63 | /** |
62 | 64 | * The JDBC drivers in the userlib folder of a project are not automatically |
63 | 65 | * correctly registered to the DriverManager. The cause is maybe the fact that |
64 | | - * the drivers are put into the project.jar on deployment. Iterating through the |
65 | | - * drivers found by ServiceLoader.load(Driver.class) will force them to load and |
66 | | - * register to the DriverManager. |
| 66 | + * the drivers are put into the project.jar on deployment. Hence, we explicitly |
| 67 | + * register the drivers. |
67 | 68 | */ |
68 | 69 | private synchronized void initializeDrivers() { |
69 | 70 | if (!hasDriversInitialized) { |
70 | 71 | ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class); |
| 72 | + List<Driver> drivers = StreamSupport.stream(loader.spliterator(), false).collect(Collectors.toList()); |
| 73 | + |
| 74 | + for (Driver driver : drivers) { |
| 75 | + try { |
| 76 | + DriverManager.registerDriver(driver); |
| 77 | + } catch (SQLException e) { |
| 78 | + throw new RuntimeException("Failed to register JDBC driver: " + driver.getClass().getName(), e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
71 | 82 | if (logNode.isTraceEnabled()) { |
72 | | - Stream<String> driverNames = StreamSupport.stream(loader.spliterator(), false) |
73 | | - .map(a -> a.getClass().getName()); |
74 | | - String logMessage = driverNames.collect(Collectors.joining(", ", "Found JDBC Drivers: ", "")); |
| 83 | + String logMessage = drivers.stream().map(a -> a.getClass().getName()) |
| 84 | + .collect(Collectors.joining(", ", "Found JDBC Drivers: ", "")); |
75 | 85 | logNode.trace(logMessage); |
76 | 86 | } |
| 87 | + |
77 | 88 | hasDriversInitialized = true; |
78 | 89 | } |
79 | 90 | } |
|
0 commit comments