Skip to content

Commit 8571c48

Browse files
authored
Merge pull request #31 from mendix/driver-loading-fix
Fix driver loading issue
2 parents f9bb8b7 + 3101257 commit 8571c48

File tree

33 files changed

+23
-12
lines changed

33 files changed

+23
-12
lines changed

build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ dependencies {
3030
compile 'com.zaxxer:HikariCP:5.0.1'
3131
compile 'org.slf4j:slf4j-api:1.7.36'
3232
compile 'com.ibm.db2.jcc:db2jcc:db2jcc4'
33-
compile 'org.hsqldb:hsqldb:2.5.0'
34-
compile 'org.mariadb.jdbc:mariadb-java-client:2.6.0'
35-
compile 'com.microsoft.sqlserver:mssql-jdbc:8.2.2.jre11'
36-
compile 'com.oracle.database.jdbc:ojdbc8:19.6.0.0'
37-
compile 'org.postgresql:postgresql:42.2.25'
38-
compile 'com.sap.cloud.db.jdbc:ngdbc:2.4.76'
33+
compile 'org.hsqldb:hsqldb:2.6.1'
34+
compile 'org.mariadb.jdbc:mariadb-java-client:2.7.5'
35+
compile 'com.microsoft.sqlserver:mssql-jdbc:9.4.0.jre11'
36+
compile 'com.oracle.database.jdbc:ojdbc8:21.5.0.0'
37+
compile 'org.postgresql:postgresql:42.3.3'
38+
compile 'com.sap.cloud.db.jdbc:ngdbc:2.11.17'
3939

4040
// UnitTesting
4141
compile group: 'junit', name: 'junit', version: '4.11'

javasource/databaseconnector/impl/JdbcConnectionManager.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import java.sql.Connection;
1010
import java.sql.Driver;
11+
import java.sql.DriverManager;
1112
import java.sql.SQLException;
13+
import java.util.List;
1214
import java.util.Map;
1315
import java.util.ServiceLoader;
1416
import java.util.concurrent.ConcurrentHashMap;
@@ -61,19 +63,28 @@ public Connection getConnection(final String jdbcUrl, final String userName, fin
6163
/**
6264
* The JDBC drivers in the userlib folder of a project are not automatically
6365
* 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.
6768
*/
6869
private synchronized void initializeDrivers() {
6970
if (!hasDriversInitialized) {
7071
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+
7182
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: ", ""));
7585
logNode.trace(logMessage);
7686
}
87+
7788
hasDriversInitialized = true;
7889
}
7990
}
-1.22 MB
Binary file not shown.
1.28 MB
Binary file not shown.
-153 KB
Binary file not shown.
194 KB
Binary file not shown.

0 commit comments

Comments
 (0)