Skip to content

Commit 44202fc

Browse files
authored
docs: simplify dialect detection in Spring Data JDBC sample (googleapis#1738)
Simplify the dialect detection for the Spring Data JDBC sample to use the built-in function for getting the dialect of a Spanner database.
1 parent 02534d7 commit 44202fc

File tree

2 files changed

+10
-43
lines changed

2 files changed

+10
-43
lines changed

samples/spring-data-jdbc/src/main/java/com/google/cloud/spanner/sample/JdbcConfiguration.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@
1616

1717
package com.google.cloud.spanner.sample;
1818

19-
import com.google.cloud.spanner.jdbc.JdbcSqlException;
20-
import com.google.rpc.Code;
19+
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
2120
import io.opentelemetry.api.OpenTelemetry;
22-
import java.util.Objects;
2321
import javax.annotation.Nonnull;
2422
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.dao.DataAccessException;
26-
import org.springframework.dao.IncorrectResultSizeDataAccessException;
2723
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
2824
import org.springframework.data.relational.core.dialect.Dialect;
2925
import org.springframework.data.relational.core.dialect.PostgresDialect;
26+
import org.springframework.jdbc.core.ConnectionCallback;
3027
import org.springframework.jdbc.core.JdbcOperations;
3128
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
3229

@@ -48,42 +45,12 @@ public Dialect jdbcDialect(@Nonnull NamedParameterJdbcOperations operations) {
4845

4946
/** Returns true if the current database is a Cloud Spanner PostgreSQL database. */
5047
public static boolean isCloudSpannerPG(JdbcOperations operations) {
51-
try {
52-
Long value =
53-
operations.queryForObject(
54-
"select 1 "
55-
+ "from information_schema.database_options "
56-
+ "where schema_name='public' "
57-
+ "and option_name='database_dialect' "
58-
+ "and option_value='POSTGRESQL'",
59-
Long.class);
60-
// Shouldn't really be anything else than 1 if the query succeeded, but this avoids complaints
61-
// from the compiler.
62-
if (Objects.equals(1L, value)) {
63-
return true;
64-
}
65-
} catch (IncorrectResultSizeDataAccessException exception) {
66-
// This indicates that it is a valid Cloud Spanner database, but not one that uses the
67-
// PostgreSQL dialect.
68-
throw new RuntimeException(
69-
"The selected Cloud Spanner database does not use the PostgreSQL dialect");
70-
} catch (DataAccessException exception) {
71-
if (exception.getCause() instanceof JdbcSqlException) {
72-
JdbcSqlException jdbcSqlException = (JdbcSqlException) exception.getCause();
73-
if (jdbcSqlException.getCode() == Code.PERMISSION_DENIED
74-
|| jdbcSqlException.getCode() == Code.NOT_FOUND) {
75-
throw new RuntimeException(
76-
"Failed to get the dialect of the Cloud Spanner database. "
77-
+ "Please check that the selected database exists and that you have permission to access it. "
78-
+ "Cause: "
79-
+ exception.getCause().getMessage(),
80-
exception.getCause());
81-
}
82-
}
83-
// ignore and fall through
84-
} catch (Throwable exception) {
85-
// ignore and fall through
86-
}
87-
return false;
48+
return Boolean.TRUE.equals(
49+
operations.execute(
50+
(ConnectionCallback<Boolean>)
51+
connection ->
52+
connection.isWrapperFor(CloudSpannerJdbcConnection.class)
53+
&& com.google.cloud.spanner.Dialect.POSTGRESQL.equals(
54+
connection.unwrap(CloudSpannerJdbcConnection.class).getDialect())));
8855
}
8956
}

samples/spring-data-jdbc/src/test/java/com/google/cloud/spanner/sample/ApplicationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ public void testRunApplication() {
817817
SpringApplication.run(Application.class).close();
818818

819819
assertEquals(
820-
44,
820+
42,
821821
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream()
822822
.filter(request -> !request.getSql().equals("SELECT 1"))
823823
.count());

0 commit comments

Comments
 (0)