Repository: googleapis/java-spanner-jdbc
Pull request #1440 (2023-12-06 19:44:20)

chore(deps): update dependency com.google.cloud:libraries-bom to v26.29.0
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [com.google.cloud:libraries-bom](https://cloud.google.com/java/docs/bom) ([source](https://togithub.com/googleapis/java-cloud-bom)) | `26.27.0` -> `26.29.0` | [![age](https://developer.mend.io/api/mc/badges/age/maven/com.google.cloud:libraries-bom/26.29.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/com.google.cloud:libraries-bom/26.29.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/com.google.cloud:libraries-bom/26.27.0/26.29.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.google.cloud:libraries-bom/26.27.0/26.29.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>googleapis/java-cloud-bom (com.google.cloud:libraries-bom)</summary>

### [`v26.29.0`](https://togithub.com/googleapis/java-cloud-bom/blob/HEAD/CHANGELOG.md#26290-2023-12-13)

##### Dependencies

-   update dependency com.google.cloud:gapic-libraries-bom to v1.26.0 ([#&#8203;6337](https://togithub.com/googleapis/java-cloud-bom/issues/6337)) ([3f9e147](https://togithub.com/googleapis/java-cloud-bom/commit/3f9e14790cc75e6b7c78962ebe7a9f51346bf04e))

### [`v26.28.0`](https://togithub.com/googleapis/java-cloud-bom/blob/HEAD/CHANGELOG.md#26280-2023-12-06)

##### Dependencies

-   update actions/github-script action to v7 ([#&#8203;6303](https://togithub.com/googleapis/java-cloud-bom/issues/6303)) ([48ab75e](https://togithub.com/googleapis/java-cloud-bom/commit/48ab75e25049d1453a09cd67174e41ea4d9090c9))
-   update actions/setup-java action to v4 ([#&#8203;6311](https://togithub.com/googleapis/java-cloud-bom/issues/6311)) ([0f42faa](https://togithub.com/googleapis/java-cloud-bom/commit/0f42faa4cfb25414faecf7560b5ebd69e03aaa65))
-   update dependency com.google.cloud:first-party-dependencies to v3.20.0 ([#&#8203;6321](https://togithub.com/googleapis/java-cloud-bom/issues/6321)) ([20ce208](https://togithub.com/googleapis/java-cloud-bom/commit/20ce208462a2cfadc231bf52c0091b1ba3d92951))
-   update dependency com.google.cloud:gapic-libraries-bom to v1.25.0 ([#&#8203;6325](https://togithub.com/googleapis/java-cloud-bom/issues/6325)) ([ba5a6e7](https://togithub.com/googleapis/java-cloud-bom/commit/ba5a6e7734caaca573dc449cf6fe770c3c03f3ce))
-   update dependency com.google.cloud:google-cloud-bigquery to v2.35.0 ([#&#8203;6322](https://togithub.com/googleapis/java-cloud-bom/issues/6322)) ([3c40c9b](https://togithub.com/googleapis/java-cloud-bom/commit/3c40c9b29301370888f5a2a4281d999c7372451d))
-   update dependency com.google.cloud:google-cloud-bigquerystorage-bom to v2.47.0 ([#&#8203;6306](https://togithub.com/googleapis/java-cloud-bom/issues/6306)) ([ae65383](https://togithub.com/googleapis/java-cloud-bom/commit/ae65383c18e7669c2e0a09e350d9233e03249fa5))
-   update dependency com.google.cloud:google-cloud-bigtable-bom to v2.30.0 ([#&#8203;6328](https://togithub.com/googleapis/java-cloud-bom/issues/6328)) ([188b3d7](https://togithub.com/googleapis/java-cloud-bom/commit/188b3d74eeac1e36df413f955219064457c66e81))
-   update dependency com.google.cloud:google-cloud-datastore-bom to v2.17.6 ([#&#8203;6317](https://togithub.com/googleapis/java-cloud-bom/issues/6317)) ([4f836aa](https://togithub.com/googleapis/java-cloud-bom/commit/4f836aaeded838abcbca0887d20d754ef861e27a))
-   update dependency com.google.cloud:google-cloud-firestore-bom to v3.15.7 ([#&#8203;6304](https://togithub.com/googleapis/java-cloud-bom/issues/6304)) ([ce72d2a](https://togithub.com/googleapis/java-cloud-bom/commit/ce72d2a0d7caa22feda6dfe0d914c7dcbcf33c5c))
-   update dependency com.google.cloud:google-cloud-logging-bom to v3.15.14 ([#&#8203;6318](https://togithub.com/googleapis/java-cloud-bom/issues/6318)) ([897eafe](https://togithub.com/googleapis/java-cloud-bom/commit/897eafe9e343e6e1aea9aaef76b3c39f3d4ba4f9))
-   update dependency com.google.cloud:google-cloud-logging-logback to v0.130.28-alpha ([#&#8203;6319](https://togithub.com/googleapis/java-cloud-bom/issues/6319)) ([65b48ad](https://togithub.com/googleapis/java-cloud-bom/commit/65b48ad605ae5d8595a8448d378d717b7fa68476))
-   update dependency com.google.cloud:google-cloud-nio to v0.127.8 ([#&#8203;6331](https://togithub.com/googleapis/java-cloud-bom/issues/6331)) ([2357cee](https://togithub.com/googleapis/java-cloud-bom/commit/2357cee69e685bcaa4c273f6a3f43bc9e9fd7c7b))
-   update dependency com.google.cloud:google-cloud-pubsub-bom to v1.125.13 ([#&#8203;6309](https://togithub.com/googleapis/java-cloud-bom/issues/6309)) ([06df6ba](https://togithub.com/googleapis/java-cloud-bom/commit/06df6ba8fd3db026a07ee477e4cecec964c9e37b))
-   update dependency com.google.cloud:google-cloud-pubsublite-bom to v1.12.19 ([#&#8203;6320](https://togithub.com/googleapis/java-cloud-bom/issues/6320)) ([7ba793b](https://togithub.com/googleapis/java-cloud-bom/commit/7ba793b21377dbccce9857f819c9f9d490764908))
-   update dependency com.google.cloud:google-cloud-spanner-bom to v6.55.0 ([#&#8203;6305](https://togithub.com/googleapis/java-cloud-bom/issues/6305)) ([5f13149](https://togithub.com/googleapis/java-cloud-bom/commit/5f131490481745532de192340eb6d639efd65563))
-   update dependency com.google.cloud:google-cloud-spanner-jdbc to v2.14.5 ([#&#8203;6302](https://togithub.com/googleapis/java-cloud-bom/issues/6302)) ([7394cc0](https://togithub.com/googleapis/java-cloud-bom/commit/7394cc0c13627aa4bd7271e759cb0b4fad888037))
-   update dependency com.google.cloud:google-cloud-spanner-jdbc to v2.14.6 ([#&#8203;6315](https://togithub.com/googleapis/java-cloud-bom/issues/6315)) ([9dbccbe](https://togithub.com/googleapis/java-cloud-bom/commit/9dbccbe210184968b678600408f6964e541a27c1))
-   update dependency com.google.cloud:google-cloud-storage-bom to v2.30.0 ([#&#8203;6323](https://togithub.com/googleapis/java-cloud-bom/issues/6323)) ([5d656a7](https://togithub.com/googleapis/java-cloud-bom/commit/5d656a7a4dc2216e6d84388fd94f3e4f2d7d49e8))
-   update dependency com.google.cloud:google-cloud-storage-bom to v2.30.1 ([#&#8203;6330](https://togithub.com/googleapis/java-cloud-bom/issues/6330)) ([fa05ea6](https://togithub.com/googleapis/java-cloud-bom/commit/fa05ea6eb21ff183bd2c48851bfc95071759b9cf))
-   update dependency commons-io:commons-io to v2.15.1 ([#&#8203;6312](https://togithub.com/googleapis/java-cloud-bom/issues/6312)) ([804f0de](https://togithub.com/googleapis/java-cloud-bom/commit/804f0de76e4fedfc11b07226b6fc76bacf673112))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/googleapis/java-spanner-jdbc).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44Ny4yIiwidXBkYXRlZEluVmVyIjoiMzcuOTMuMSIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->



Patch:
diff --git a/pom.xml b/pom.xml
index 90ac8a5..1f01bee 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@
   <parent>
     <groupId>com.google.cloud</groupId>
     <artifactId>google-cloud-shared-config</artifactId>
-    <version>1.6.1</version>
+    <version>1.7.1</version>
   </parent>
   <developers>
     <developer>
diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml
index 8a87cff..df372f2 100644
--- a/samples/snippets/pom.xml
+++ b/samples/snippets/pom.xml
@@ -30,7 +30,7 @@
       <dependency>
         <groupId>com.google.cloud</groupId>
         <artifactId>libraries-bom</artifactId>
-        <version>26.27.0</version>
+        <version>26.29.0</version>
         <type>pom</type>
         <scope>import</scope>
       </dependency>
diff --git a/samples/spring-data-jdbc/pom.xml b/samples/spring-data-jdbc/pom.xml
index d1dd574..cf500a9 100644
--- a/samples/spring-data-jdbc/pom.xml
+++ b/samples/spring-data-jdbc/pom.xml
@@ -30,7 +30,7 @@
       <dependency>
         <groupId>com.google.cloud</groupId>
         <artifactId>libraries-bom</artifactId>
-        <version>26.27.0</version>
+        <version>26.29.0</version>
         <scope>import</scope>
         <type>pom</type>
       </dependency>
@@ -52,7 +52,7 @@
     <dependency>
       <groupId>org.postgresql</groupId>
       <artifactId>postgresql</artifactId>
-      <version>42.7.0</version>
+      <version>42.7.1</version>
     </dependency>
 
     <dependency>
diff --git a/samples/spring-data-mybatis/pom.xml b/samples/spring-data-mybatis/pom.xml
index 4dbd092..28342f9 100644
--- a/samples/spring-data-mybatis/pom.xml
+++ b/samples/spring-data-mybatis/pom.xml
@@ -35,7 +35,7 @@
       <dependency>
         <groupId>com.google.cloud</groupId>
         <artifactId>libraries-bom</artifactId>
-        <version>26.27.0</version>
+        <version>26.29.0</version>
         <scope>import</scope>
         <type>pom</type>
       </dependency>
@@ -62,7 +62,7 @@
     <dependency>
       <groupId>org.postgresql</groupId>
       <artifactId>postgresql</artifactId>
-      <version>42.7.0</version>
+      <version>42.7.1</version>
     </dependency>
 
     <dependency>
diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java
index 9248dac..4cef6b3 100644
--- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java
+++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java
@@ -29,6 +29,7 @@ import com.google.cloud.spanner.connection.Connection;
 import com.google.cloud.spanner.connection.ConnectionOptions;
 import com.google.cloud.spanner.connection.SavepointSupport;
 import com.google.cloud.spanner.connection.TransactionMode;
+import com.google.common.base.Strings;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterators;
 import java.sql.Array;
@@ -57,14 +58,42 @@ class JdbcConnection extends AbstractJdbcConnection {
       "Only result sets with concurrency CONCUR_READ_ONLY are supported";
   private static final String ONLY_CLOSE_CURSORS_AT_COMMIT =
       "Only result sets with holdability CLOSE_CURSORS_AT_COMMIT are supported";
-  static final String IS_VALID_QUERY = "SELECT 1";
+
+  /**
+   * This query is used to check the aliveness of the connection if legacy alive check has been
+   * enabled. As Cloud Spanner JDBC connections do not maintain a physical or logical connection to
+   * Cloud Spanner, there is also no point in repeatedly executing a simple query to check whether a
+   * connection is alive. Instead, we rely on the result from the initial query to Spanner that
+   * determines the dialect to determine whether the connection is alive or not. This result is
+   * cached for all JDBC connections using the same {@link com.google.cloud.spanner.Spanner}
+   * instance.
+   *
+   * <p>The legacy {@link #isValid(int)} check using a SELECT 1 statement can be enabled by setting
+   * the System property spanner.jdbc.use_legacy_is_valid_check to true or setting the environment
+   * variable SPANNER_JDBC_USE_LEGACY_IS_VALID_CHECK to true.
+   */
+  static final String LEGACY_IS_VALID_QUERY = "SELECT 1";
 
   static final ImmutableList<String> NO_GENERATED_KEY_COLUMNS = ImmutableList.of();
 
   private Map<String, Class<?>> typeMap = new HashMap<>();
 
+  private final boolean useLegacyIsValidCheck;
+
   JdbcConnection(String connectionUrl, ConnectionOptions options) throws SQLException {
     super(connectionUrl, options);
+    this.useLegacyIsValidCheck = useLegacyValidCheck();
+  }
+
+  static boolean useLegacyValidCheck() {
+    String value = System.getProperty("spanner.jdbc.use_legacy_is_valid_check");
+    if (Strings.isNullOrEmpty(value)) {
+      value = System.getenv("SPANNER_JDBC_USE_LEGACY_IS_VALID_CHECK");
+    }
+    if (!Strings.isNullOrEmpty(value)) {
+      return Boolean.parseBoolean(value);
+    }
+    return false;
   }
 
   @Override
@@ -347,23 +376,38 @@ class JdbcConnection extends AbstractJdbcConnection {
     this.typeMap = new HashMap<>(map);
   }
 
+  boolean isUseLegacyIsValidCheck() {
+    return useLegacyIsValidCheck;
+  }
+
   @Override
   public boolean isValid(int timeout) throws SQLException {
     JdbcPreconditions.checkArgument(timeout >= 0, "timeout must be >= 0");
     if (!isClosed()) {
+      if (isUseLegacyIsValidCheck()) {
+        return legacyIsValid(timeout);
+      }
       try {
-        Statement statement = createStatement();
-        statement.setQueryTimeout(timeout);
-        try (ResultSet rs = statement.executeQuery(IS_VALID_QUERY)) {
-          if (rs.next()) {
-            if (rs.getLong(1) == 1L) {
-              return true;
-            }
+        return getDialect() != null;
+      } catch (Exception ignore) {
+        // ignore and fall through.
+      }
+    }
+    return false;
+  }
+
+  private boolean legacyIsValid(int timeout) throws SQLException {
+    try (Statement statement = createStatement()) {
+      statement.setQueryTimeout(timeout);
+      try (ResultSet rs = statement.executeQuery(LEGACY_IS_VALID_QUERY)) {
+        if (rs.next()) {
+          if (rs.getLong(1) == 1L) {
+            return true;
           }
         }
-      } catch (SQLException e) {
-        // ignore
       }
+    } catch (SQLException e) {
+      // ignore and fall through.
     }
     return false;
   }


Test patch:
diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java
index ff811f3..8d26bb3 100644
--- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java
+++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java
@@ -502,7 +502,7 @@ public class JdbcConnectionTest {
         mock(com.google.cloud.spanner.connection.Connection.class);
     when(spannerConnection.getDialect()).thenReturn(dialect);
     when(options.getConnection()).thenReturn(spannerConnection);
-    Statement statement = Statement.of(JdbcConnection.IS_VALID_QUERY);
+    Statement statement = Statement.of(JdbcConnection.LEGACY_IS_VALID_QUERY);
 
     // Verify that an opened connection that returns a result set is valid.
     try (JdbcConnection connection = new JdbcConnection("url", options)) {
@@ -517,7 +517,7 @@ public class JdbcConnectionTest {
       }
 
       // Now let the query return an error. isValid should now return false.
-      when(spannerConnection.executeQuery(statement))
+      when(spannerConnection.getDialect())
           .thenThrow(
               SpannerExceptionFactory.newSpannerException(
                   ErrorCode.ABORTED, "the current transaction has been aborted"));