1616
1717package 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 ;
2120import io .opentelemetry .api .OpenTelemetry ;
22- import java .util .Objects ;
2321import javax .annotation .Nonnull ;
2422import org .springframework .context .annotation .Configuration ;
25- import org .springframework .dao .DataAccessException ;
26- import org .springframework .dao .IncorrectResultSizeDataAccessException ;
2723import org .springframework .data .jdbc .repository .config .AbstractJdbcConfiguration ;
2824import org .springframework .data .relational .core .dialect .Dialect ;
2925import org .springframework .data .relational .core .dialect .PostgresDialect ;
26+ import org .springframework .jdbc .core .ConnectionCallback ;
3027import org .springframework .jdbc .core .JdbcOperations ;
3128import 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}
0 commit comments