Skip to content

Commit 0e557ef

Browse files
feat: allow unknown properties in connection url with lenient mode (#284)
* feat: allow unknown properties in connection url with lenient mode * Update src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com> * fix: add credentials to prevent the use of env credentials Co-authored-by: yoshi-code-bot <70984784+yoshi-code-bot@users.noreply.github.com>
1 parent 9f559d8 commit 0e557ef

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/main/java/com/google/cloud/spanner/jdbc/JdbcDriver.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.sql.DriverPropertyInfo;
2929
import java.sql.SQLException;
3030
import java.sql.SQLFeatureNotSupportedException;
31+
import java.sql.SQLWarning;
3132
import java.util.Map.Entry;
3233
import java.util.Properties;
3334
import java.util.logging.Logger;
@@ -172,7 +173,11 @@ public Connection connect(String url, Properties info) throws SQLException {
172173
// Connection API
173174
String connectionUri = appendPropertiesToUrl(url.substring(5), info);
174175
ConnectionOptions options = ConnectionOptions.newBuilder().setUri(connectionUri).build();
175-
return new JdbcConnection(url, options);
176+
JdbcConnection connection = new JdbcConnection(url, options);
177+
if (options.getWarnings() != null) {
178+
connection.pushWarning(new SQLWarning(options.getWarnings()));
179+
}
180+
return connection;
176181
}
177182
} catch (SpannerException e) {
178183
throw JdbcSqlExceptionFactory.of(e);

src/test/java/com/google/cloud/spanner/jdbc/JdbcDriverTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,32 @@ public String apply(DriverPropertyInfo input) {
161161
});
162162
assertThat(driverPropertyNames).containsExactlyElementsIn(validConnectionPropertyNames);
163163
}
164+
165+
@Test
166+
public void testLenient() throws SQLException {
167+
// With lenient=true the driver should accept unknown properties and only generate a warning.
168+
try (Connection connection =
169+
DriverManager.getConnection(
170+
String.format(
171+
"jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;lenient=true;foo=bar",
172+
server.getPort(), TEST_KEY_PATH))) {
173+
assertThat(connection.isClosed()).isFalse();
174+
assertThat((Throwable) connection.getWarnings()).isNotNull();
175+
assertThat(connection.getWarnings().getMessage()).contains("foo");
176+
}
177+
178+
// Without lenient the driver should throw an exception for unknown properties.
179+
try (Connection connection =
180+
DriverManager.getConnection(
181+
String.format(
182+
"jdbc:cloudspanner://localhost:%d/projects/p/instances/i/databases/d?usePlainText=true;credentials=%s;foo=bar",
183+
server.getPort(), TEST_KEY_PATH))) {
184+
fail("missing expected exception");
185+
} catch (SQLException e) {
186+
assertThat((Throwable) e).isInstanceOf(JdbcSqlException.class);
187+
JdbcSqlException jdbc = (JdbcSqlException) e;
188+
assertThat(jdbc.getMessage()).contains("foo");
189+
assertThat(jdbc.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
190+
}
191+
}
164192
}

0 commit comments

Comments
 (0)