postgis-java icon indicating copy to clipboard operation
postgis-java copied to clipboard

DriverWrapper#mangleURL should return null (not throw an exception) ...

Open soc opened this issue 3 years ago • 4 comments

... 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?

soc avatar Jan 21 '22 14:01 soc

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.

AndreasWBartels avatar Jan 22 '22 01:01 AndreasWBartels

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?

phillipross avatar Jan 22 '22 07:01 phillipross

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? :(

soc avatar Jan 23 '22 00:01 soc

Nevermind, was easier than expected. https://github.com/postgis/postgis-java/pull/114

soc avatar Jan 23 '22 01:01 soc