Reputation: 21
I have a Spring Boot application that connects to multiple databases - Oracle, SQLServer, DB2, MySQL etc.
I import the requisite JDBC drivers via Gradle dependencies:
implementation 'mysql:mysql-connector-java:8.0.22'
implementation ('com.microsoft.sqlserver:mssql-jdbc:8.4.1.jre8')
implementation group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '19.9.0.0'
implementation group: 'com.ibm.db2', name: 'jcc', version: '11.5.6.0'
and so forth. They all work fine.
I now have a requirement to connect to DB2 on an AS400 server. There are two potential drivers: one via DBConnect with a license, or the open source JT400 library. I am using the latter.
I added this dependency statement in the same build.gradle file:
implementation group: 'net.sf.jt400', name: 'jt400', version: '10.6'
Refreshing dependencies downloads the library as expected. I can even see it listed in the classpath declaration when starting the app:
[...]681a/jjwt-0.9.1.jar:/home/paul/.gradle/caches/modules-2/files-2.1/net.sf.jt400/jt400/10.6/3f391a43632a93874dd3f544d7cb5a91b7a02380/jt400-10.6.jar:/home/paul/.gradle/caches/modules-2/files-2.1/...
If I attempt to connect to an AS400 URL, however, I get a "No suitable driver found" when attempting the connection.
I can debug into the application when the connection is attempted, and try Class.forName("com.ibm.as400.access.AS400JDBCDriver")
and receive a class not found exception. If I open the referenced jar file, the named class exists as expected.
I built a small sample application with just this particular jar on the command line, and it executes perfectly (the response is expected, as I'm not pointing at an actual AS400 server yet).
paul@paul-adm-dev:~/$ java -cp .:/home/paul/.gradle/caches/modules-2/files-2.1/net.sf.jt400/jt400/10.6/3f391a43632a93874dd3f544d7cb5a91b7a02380/jt400-10.6.jar AS400ConnectionExample
java.sql.SQLException: The application requester cannot establish the connection. (Connection refused (Connection refused))
I tried changing the dependency to Compile and to the obsolete Runtime, to no avail. I even tried the import in other sub-projects that make up the overall application, but always with the same result.
I am stumped. What might cause this one single library to be not loaded?
Upvotes: 1
Views: 1640
Reputation: 21
Solved. Sigh.
This particular driver, in spite of its own documentation, does NOT auto-register with the SQL driver manager.
Adding Class.forName("com.ibm.as400.access.AS400JDBCDriver")
in a static initializer in the component tasked with creating Connections solved the issue. No idea why it failed to do so in a debug session evaluation window, but at least now I know why this driver didn't 'just work' as the others do.
Upvotes: 1