Skip to content

Commit 01eade8

Browse files
authored
docs: simplify dialect detection for MyBatis sample (googleapis#1739)
1 parent f229a62 commit 01eade8

File tree

2 files changed

+10
-43
lines changed

2 files changed

+10
-43
lines changed

samples/spring-data-mybatis/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,55 +16,22 @@
1616

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

19-
import com.google.cloud.spanner.jdbc.JdbcSqlException;
20-
import com.google.rpc.Code;
21-
import java.util.Objects;
19+
import com.google.cloud.spanner.jdbc.CloudSpannerJdbcConnection;
2220
import org.springframework.context.annotation.Configuration;
23-
import org.springframework.dao.DataAccessException;
24-
import org.springframework.dao.IncorrectResultSizeDataAccessException;
21+
import org.springframework.jdbc.core.ConnectionCallback;
2522
import org.springframework.jdbc.core.JdbcOperations;
2623

2724
@Configuration
2825
public class JdbcConfiguration {
2926

3027
/** Returns true if the current database is a Cloud Spanner PostgreSQL database. */
3128
public static boolean isCloudSpannerPG(JdbcOperations operations) {
32-
try {
33-
Long value =
34-
operations.queryForObject(
35-
"select 1 "
36-
+ "from information_schema.database_options "
37-
+ "where schema_name='public' "
38-
+ "and option_name='database_dialect' "
39-
+ "and option_value='POSTGRESQL'",
40-
Long.class);
41-
// Shouldn't really be anything else than 1 if the query succeeded, but this avoids complaints
42-
// from the compiler.
43-
if (Objects.equals(1L, value)) {
44-
return true;
45-
}
46-
} catch (IncorrectResultSizeDataAccessException exception) {
47-
// This indicates that it is a valid Cloud Spanner database, but not one that uses the
48-
// PostgreSQL dialect.
49-
throw new RuntimeException(
50-
"The selected Cloud Spanner database does not use the PostgreSQL dialect");
51-
} catch (DataAccessException exception) {
52-
if (exception.getCause() instanceof JdbcSqlException) {
53-
JdbcSqlException jdbcSqlException = (JdbcSqlException) exception.getCause();
54-
if (jdbcSqlException.getCode() == Code.PERMISSION_DENIED
55-
|| jdbcSqlException.getCode() == Code.NOT_FOUND) {
56-
throw new RuntimeException(
57-
"Failed to get the dialect of the Cloud Spanner database. "
58-
+ "Please check that the selected database exists and that you have permission to access it. "
59-
+ "Cause: "
60-
+ exception.getCause().getMessage(),
61-
exception.getCause());
62-
}
63-
}
64-
// ignore and fall through
65-
} catch (Throwable exception) {
66-
// ignore and fall through
67-
}
68-
return false;
29+
return Boolean.TRUE.equals(
30+
operations.execute(
31+
(ConnectionCallback<Boolean>)
32+
connection ->
33+
connection.isWrapperFor(CloudSpannerJdbcConnection.class)
34+
&& com.google.cloud.spanner.Dialect.POSTGRESQL.equals(
35+
connection.unwrap(CloudSpannerJdbcConnection.class).getDialect())));
6936
}
7037
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ public void testRunApplication() {
876876
Application.main(new String[] {});
877877

878878
assertEquals(
879-
40,
879+
39,
880880
mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).stream()
881881
.filter(
882882
request ->

0 commit comments

Comments
 (0)