postgis-java
postgis-java copied to clipboard
DriverWrapper#mangleURL should return null (not throw an exception) ...
... if the JDBC driver is not applicable to the connection string.
Offending code: https://github.com/postgis/postgis-java/blob/main/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java#L265
Background:
If one has multiple JDBC driver registered, DriverManager.getConnection
is called on each one of them until one driver declares it can handle the URL.
If that method throws an exception, then the "reason" is overwritten if multiple drivers are registered (e. g. Oracle, PostGis).
This causes an access attempt to an OracleDB with wrong password to return an Postgres error message.
This is because Oracle is tried and throws (correct, wrong password) and then Postgis can't handle it and also throws (not correct, should return null
), thereby overwriting the exception and hiding the underlying problem:
java.sql.SQLException: Unknown protocol or subprotocol in url jdbc:oracle:thin:@oracle16.example.com:1521:oracle16
at net.postgis.jdbc.DriverWrapper.mangleURL(DriverWrapper.java:265)
at net.postgis.jdbc.DriverWrapper.connect(DriverWrapper.java:159)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:190)
If you agree with the assessment, would you accept a PR?
part of the comment from Driver.connect()
* Attempts to make a database connection to the given URL.
* The driver should return "null" if it realizes it is the wrong kind
* of driver to connect to the given URL. This will be common, as when
* the JDBC driver manager is asked to connect to a given URL it passes
* the URL to each loaded driver in turn.
Thanks for identifying this issue! A PR to fix would be great. Would it also be possible to include a test that demonstrates the problem?
Mhhh, one could probable "fake" the drivers, register them, and make sure that the logic ends up as intended.
@phillipross yes, will do!
.... welp, but we probably would also need some database to connect to? :(
Nevermind, was easier than expected. https://github.com/postgis/postgis-java/pull/114