From bc9265afcfb791ead92c6c0337a07292de6e2128 Mon Sep 17 00:00:00 2001 From: Simon Ochsenreither Date: Sun, 23 Jan 2022 01:56:21 +0100 Subject: [PATCH 1/2] Treat unsupported URLs as specified in `Driver#connect` to avoid overwriting the "real" exception --- .../java/net/postgis/jdbc/DriverWrapper.java | 8 +++-- .../jdbc/DriverConnectBehaviorTest.java | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 postgis-jdbc/src/test/java/net/postgis/jdbc/DriverConnectBehaviorTest.java diff --git a/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java b/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java index 01bb858..eff8c9b 100644 --- a/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java +++ b/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java @@ -145,18 +145,20 @@ private static TypesAdder loadTypesAdder(final String version) throws SQLExcepti /** * Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes(). * - * A side-effect of this method is that the specified url parameter may be be changed + * A side-effect of this method is that the specified url parameter may be changed * * @param url the URL of the database to connect to (may be changed as a side-effect of this method) * @param info a list of arbitrary tag/value pairs as connection arguments - * @return a connection to the URL or null if it isnt us + * @return a connection to the URL or null if the driver does not support the subprotocol specified in the URL * @exception SQLException if a database access error occurs * * @see java.sql.Driver#connect * @see org.postgresql.Driver */ public java.sql.Connection connect(String url, final Properties info) throws SQLException { - url = mangleURL(url); + if (!acceptsURL(url)) { + return null; + } Connection result = super.connect(url, info); typesAdder.addGT(result, useLW(result)); return result; diff --git a/postgis-jdbc/src/test/java/net/postgis/jdbc/DriverConnectBehaviorTest.java b/postgis-jdbc/src/test/java/net/postgis/jdbc/DriverConnectBehaviorTest.java new file mode 100644 index 0000000..a81aa47 --- /dev/null +++ b/postgis-jdbc/src/test/java/net/postgis/jdbc/DriverConnectBehaviorTest.java @@ -0,0 +1,32 @@ +package net.postgis.jdbc; + +import org.junit.Assert; +import org.junit.Test; + +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DriverConnectBehaviorTest { + + @Test + public void testThatPostGisDoesNotOverwriteSavedExceptionForUnsupportedConnectionString() { + try { + DriverManager.getConnection("jdbc:missing"); + } catch (SQLException e) { + // This should not be "Unknown protocol or subprotocol in url jdbc:missing", which + // would indicate that PostGIS threw an exception instead of returning `null` from + // the `connect` method for an unsupported connection string. + // (This is documented in `java.sql.Driver.connect`.) + // + // The former behavior is not desirable as throwing an exception causes a previously + // saved exception from a "better fitting" driver to be overwritten by PostGis, despite + // PostGis not actually being able to handle the connection. + // + // (Imagine an Oracle connection string with a wrong password, in which the Oracle + // driver's exception regarding the wrong password would be replaced with a generic + // nonsensical PostGis exception.) + Assert.assertEquals("No suitable driver found for jdbc:missing", e.getMessage()); + } + } + +} From 34830e87d8d7a7dc19df159c203dcf48f0e9223a Mon Sep 17 00:00:00 2001 From: Simon Ochsenreither Date: Sun, 23 Jan 2022 01:58:35 +0100 Subject: [PATCH 2/2] Drop documentation on URL being changed --- .../src/main/java/net/postgis/jdbc/DriverWrapper.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java b/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java index eff8c9b..60273ac 100644 --- a/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java +++ b/postgis-jdbc/src/main/java/net/postgis/jdbc/DriverWrapper.java @@ -145,9 +145,7 @@ private static TypesAdder loadTypesAdder(final String version) throws SQLExcepti /** * Creates a postgresql connection, and then adds the PostGIS data types to it calling addpgtypes(). * - * A side-effect of this method is that the specified url parameter may be changed - * - * @param url the URL of the database to connect to (may be changed as a side-effect of this method) + * @param url the URL of the database to connect to * @param info a list of arbitrary tag/value pairs as connection arguments * @return a connection to the URL or null if the driver does not support the subprotocol specified in the URL * @exception SQLException if a database access error occurs @@ -186,7 +184,7 @@ protected boolean useLW(final Connection result) { * A side-effect of this method is that the specified url parameter may be be changed * * @see java.sql.Driver#acceptsURL - * @param url the URL of the driver (may be changed as a side-effect of this method) + * @param url the URL of the driver * @return true if this driver accepts the given URL */ public boolean acceptsURL(String url) {