Repository: googleapis/java-spanner-jdbc
Pull request #1235 (2023-05-16 14:29:21)

chore(deps): update dependency com.google.cloud:libraries-bom to v26.15.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.14.0` -> `26.15.0` | [![age](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/26.15.0/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/26.15.0/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/26.15.0/compatibility-slim/26.14.0)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/maven/com.google.cloud:libraries-bom/26.15.0/confidence-slim/26.14.0)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>googleapis/java-cloud-bom</summary>

### [`v26.15.0`](https://togithub.com/googleapis/java-cloud-bom/blob/HEAD/CHANGELOG.md#&#8203;26150-httpsgithubcomgoogleapisjava-cloud-bomcomparev26140v26150-2023-05-16)

[Compare Source](https://togithub.com/googleapis/java-cloud-bom/compare/v26.14.0...v26.15.0)

##### Dependencies

-   update dependency com.google.cloud:first-party-dependencies to v3.9.0 ([#&#8203;5974](https://togithub.com/googleapis/java-cloud-bom/issues/5974)) ([3730711](https://togithub.com/googleapis/java-cloud-bom/commit/3730711dbf7616fcc914a65a1e956b26a13beee3))
-   update dependency com.google.cloud:gapic-libraries-bom to v1.11.0 ([#&#8203;5981](https://togithub.com/googleapis/java-cloud-bom/issues/5981)) ([26d29e3](https://togithub.com/googleapis/java-cloud-bom/commit/26d29e38e2ee6e46fce204e001f6ac292c1d1ab3))
-   update dependency com.google.cloud:google-cloud-bigquery to v2.26.0 ([#&#8203;5989](https://togithub.com/googleapis/java-cloud-bom/issues/5989)) ([7cbc31a](https://togithub.com/googleapis/java-cloud-bom/commit/7cbc31ac30d95fdd31b6e94d7af79fc1a021b059))
-   update dependency com.google.cloud:google-cloud-bigquerystorage-bom to v2.37.0 ([#&#8203;5986](https://togithub.com/googleapis/java-cloud-bom/issues/5986)) ([e72a9b9](https://togithub.com/googleapis/java-cloud-bom/commit/e72a9b9a7282e87d68de1465e61310814476debe))
-   update dependency com.google.cloud:google-cloud-bigtable-bom to v2.23.0 ([#&#8203;5975](https://togithub.com/googleapis/java-cloud-bom/issues/5975)) ([7670085](https://togithub.com/googleapis/java-cloud-bom/commit/7670085733f6803849fa027da53d956d55a0afa6))
-   update dependency com.google.cloud:google-cloud-bigtable-bom to v2.23.1 ([#&#8203;5982](https://togithub.com/googleapis/java-cloud-bom/issues/5982)) ([6555f26](https://togithub.com/googleapis/java-cloud-bom/commit/6555f26029d1cbfa21898cd0b25cd19728d588c6))
-   update dependency com.google.cloud:google-cloud-datastore-bom to v2.14.6 ([#&#8203;5976](https://togithub.com/googleapis/java-cloud-bom/issues/5976)) ([4b33ff3](https://togithub.com/googleapis/java-cloud-bom/commit/4b33ff3331897654215d306925aa10c11bfff5a5))
-   update dependency com.google.cloud:google-cloud-firestore-bom to v3.11.0 ([#&#8203;5990](https://togithub.com/googleapis/java-cloud-bom/issues/5990)) ([34e6d13](https://togithub.com/googleapis/java-cloud-bom/commit/34e6d13eb868a2578752b4468dc0df03e27768c2))
-   update dependency com.google.cloud:google-cloud-logging-bom to v3.15.0 ([#&#8203;5973](https://togithub.com/googleapis/java-cloud-bom/issues/5973)) ([6b5e212](https://togithub.com/googleapis/java-cloud-bom/commit/6b5e21232feb9b44fa9943ac7efb78544d88cae0))
-   update dependency com.google.cloud:google-cloud-logging-bom to v3.15.1 ([#&#8203;5983](https://togithub.com/googleapis/java-cloud-bom/issues/5983)) ([ab7420a](https://togithub.com/googleapis/java-cloud-bom/commit/ab7420a3e05b121c4a4f12d0ef1a0029aa116738))
-   update dependency com.google.cloud:google-cloud-logging-logback to v0.130.12-alpha ([#&#8203;5987](https://togithub.com/googleapis/java-cloud-bom/issues/5987)) ([903db64](https://togithub.com/googleapis/java-cloud-bom/commit/903db645e4cc353cfdf4bf1f2ddc03de0c029bba))
-   update dependency com.google.cloud:google-cloud-nio to v0.126.15 ([#&#8203;5980](https://togithub.com/googleapis/java-cloud-bom/issues/5980)) ([c30c6db](https://togithub.com/googleapis/java-cloud-bom/commit/c30c6db9a14ed16039f55cdb3ae53f736c300bda))
-   update dependency com.google.cloud:google-cloud-pubsub-bom to v1.123.12 ([#&#8203;5984](https://togithub.com/googleapis/java-cloud-bom/issues/5984)) ([f0c8b46](https://togithub.com/googleapis/java-cloud-bom/commit/f0c8b46d8003d9eabd72f99e37a3c0e66cd00521))
-   update dependency com.google.cloud:google-cloud-pubsublite-bom to v1.12.5 ([#&#8203;5988](https://togithub.com/googleapis/java-cloud-bom/issues/5988)) ([d537c53](https://togithub.com/googleapis/java-cloud-bom/commit/d537c532eef156625281e4bf8ef4712c97af4ebb))
-   update dependency com.google.cloud:google-cloud-spanner-bom to v6.42.0 ([#&#8203;5991](https://togithub.com/googleapis/java-cloud-bom/issues/5991)) ([530dc1b](https://togithub.com/googleapis/java-cloud-bom/commit/530dc1b7358b980dad755d1fe4624e0f46c89f4d))
-   update dependency com.google.cloud:google-cloud-spanner-jdbc to v2.9.16 ([#&#8203;5985](https://togithub.com/googleapis/java-cloud-bom/issues/5985)) ([f47314a](https://togithub.com/googleapis/java-cloud-bom/commit/f47314a7f5590983a0b3ebb1c0394aa2572fa94a))
-   update dependency com.google.cloud:google-cloud-storage-bom to v2.22.2 ([#&#8203;5978](https://togithub.com/googleapis/java-cloud-bom/issues/5978)) ([f61a097](https://togithub.com/googleapis/java-cloud-bom/commit/f61a097ee841ec5176f797c86a4f60ba519110bb))

</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://app.renovatebot.com/dashboard#github/googleapis/java-spanner-jdbc).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS40OC4yIiwidXBkYXRlZEluVmVyIjoiMzUuNDguMiJ9-->



Patch:
diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml
index 35478af..aadf54f 100644
--- a/.github/.OwlBot.lock.yaml
+++ b/.github/.OwlBot.lock.yaml
@@ -13,4 +13,4 @@
 # limitations under the License.
 docker:
   image: gcr.io/cloud-devrel-public-resources/owlbot-java:latest
-  digest: sha256:8dd8395defb6a5069b0b10c435058bf13980606ba1967e2b3925ed50fc3cb22f
+  digest: sha256:ad9cabee4c022f1aab04a71332369e0c23841062124818a4490f73337f790337
diff --git a/.github/release-trigger.yml b/.github/release-trigger.yml
index d4ca941..5e66248 100644
--- a/.github/release-trigger.yml
+++ b/.github/release-trigger.yml
@@ -1,1 +1,2 @@
 enabled: true
+multiScmName: java-spanner-jdbc
diff --git a/clirr-ignored-differences.xml b/clirr-ignored-differences.xml
index 235cb90..a92ad53 100644
--- a/clirr-ignored-differences.xml
+++ b/clirr-ignored-differences.xml
@@ -6,4 +6,14 @@
     <className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
     <method>com.google.cloud.spanner.Dialect getDialect()</method>
   </difference>
+  <difference>
+    <differenceType>7012</differenceType>
+    <className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
+    <method>com.google.cloud.spanner.connection.SavepointSupport getSavepointSupport()</method>
+  </difference>
+  <difference>
+    <differenceType>7012</differenceType>
+    <className>com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection</className>
+    <method>void setSavepointSupport(com.google.cloud.spanner.connection.SavepointSupport)</method>
+  </difference>
 </differences>
diff --git a/pom.xml b/pom.xml
index 9390393..0db72ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -69,7 +69,7 @@
       <dependency>
         <groupId>com.google.cloud</groupId>
         <artifactId>google-cloud-shared-dependencies</artifactId>
-        <version>3.9.0</version>
+        <version>3.10.1</version>
         <type>pom</type>
         <scope>import</scope>
       </dependency>
@@ -393,7 +393,7 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-project-info-reports-plugin</artifactId>
-        <version>3.4.3</version>
+        <version>3.4.4</version>
         <reportSets>
           <reportSet>
             <reports>
diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml
index fcbbc8e..fa6b15a 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.14.0</version>
+        <version>26.15.0</version>
         <type>pom</type>
         <scope>import</scope>
       </dependency>
diff --git a/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcConnection.java b/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcConnection.java
index 1a6cfe2..33cf3bc 100644
--- a/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcConnection.java
+++ b/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcConnection.java
@@ -28,7 +28,6 @@ import java.sql.SQLClientInfoException;
 import java.sql.SQLException;
 import java.sql.SQLWarning;
 import java.sql.SQLXML;
-import java.sql.Savepoint;
 import java.sql.Struct;
 import java.util.Properties;
 import java.util.concurrent.Executor;
@@ -42,7 +41,6 @@ abstract class AbstractJdbcConnection extends AbstractJdbcWrapper
       "Only isolation level TRANSACTION_SERIALIZABLE is supported";
   private static final String ONLY_CLOSE_ALLOWED =
       "Only holdability CLOSE_CURSORS_AT_COMMIT is supported";
-  private static final String SAVEPOINTS_UNSUPPORTED = "Savepoints are not supported";
   private static final String SQLXML_UNSUPPORTED = "SQLXML is not supported";
   private static final String STRUCTS_UNSUPPORTED = "Structs are not supported";
   private static final String ABORT_UNSUPPORTED = "Abort is not supported";
@@ -163,26 +161,6 @@ abstract class AbstractJdbcConnection extends AbstractJdbcWrapper
     lastWarning = null;
   }
 
-  @Override
-  public Savepoint setSavepoint() throws SQLException {
-    return checkClosedAndThrowUnsupported(SAVEPOINTS_UNSUPPORTED);
-  }
-
-  @Override
-  public Savepoint setSavepoint(String name) throws SQLException {
-    return checkClosedAndThrowUnsupported(SAVEPOINTS_UNSUPPORTED);
-  }
-
-  @Override
-  public void rollback(Savepoint savepoint) throws SQLException {
-    checkClosedAndThrowUnsupported(SAVEPOINTS_UNSUPPORTED);
-  }
-
-  @Override
-  public void releaseSavepoint(Savepoint savepoint) throws SQLException {
-    checkClosedAndThrowUnsupported(SAVEPOINTS_UNSUPPORTED);
-  }
-
   @Override
   public SQLXML createSQLXML() throws SQLException {
     return checkClosedAndThrowUnsupported(SQLXML_UNSUPPORTED);
diff --git a/src/main/java/com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection.java b/src/main/java/com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection.java
index 679fa87..588e8e7 100644
--- a/src/main/java/com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection.java
+++ b/src/main/java/com/google/cloud/spanner/jdbc/CloudSpannerJdbcConnection.java
@@ -25,6 +25,7 @@ import com.google.cloud.spanner.Mutation;
 import com.google.cloud.spanner.ResultSet;
 import com.google.cloud.spanner.TimestampBound;
 import com.google.cloud.spanner.connection.AutocommitDmlMode;
+import com.google.cloud.spanner.connection.SavepointSupport;
 import com.google.cloud.spanner.connection.TransactionMode;
 import java.sql.Connection;
 import java.sql.SQLException;
@@ -256,6 +257,12 @@ public interface CloudSpannerJdbcConnection extends Connection {
    */
   void setRetryAbortsInternally(boolean retryAbortsInternally) throws SQLException;
 
+  /** Returns the current savepoint support for this connection. */
+  SavepointSupport getSavepointSupport() throws SQLException;
+
+  /** Sets how savepoints should be supported on this connection. */
+  void setSavepointSupport(SavepointSupport savepointSupport) throws SQLException;
+
   /**
    * Writes the specified mutation directly to the database and commits the change. The value is
    * readable after the successful completion of this method. Writing multiple mutations to a
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 867695f..a710ac8 100644
--- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java
+++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcConnection.java
@@ -23,6 +23,7 @@ import com.google.cloud.spanner.SpannerException;
 import com.google.cloud.spanner.TimestampBound;
 import com.google.cloud.spanner.connection.AutocommitDmlMode;
 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.collect.Iterators;
 import java.sql.Array;
@@ -33,6 +34,7 @@ import java.sql.NClob;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Savepoint;
 import java.sql.Statement;
 import java.sql.Timestamp;
 import java.util.HashMap;
@@ -402,6 +404,70 @@ class JdbcConnection extends AbstractJdbcConnection {
     return "";
   }
 
+  @Override
+  public SavepointSupport getSavepointSupport() throws SQLException {
+    checkClosed();
+    return getSpannerConnection().getSavepointSupport();
+  }
+
+  @Override
+  public void setSavepointSupport(SavepointSupport savepointSupport) throws SQLException {
+    checkClosed();
+    try {
+      getSpannerConnection().setSavepointSupport(savepointSupport);
+    } catch (SpannerException e) {
+      throw JdbcSqlExceptionFactory.of(e);
+    }
+  }
+
+  @Override
+  public Savepoint setSavepoint() throws SQLException {
+    checkClosed();
+    try {
+      JdbcSavepoint savepoint = JdbcSavepoint.unnamed();
+      getSpannerConnection().savepoint(savepoint.internalGetSavepointName());
+      return savepoint;
+    } catch (SpannerException e) {
+      throw JdbcSqlExceptionFactory.of(e);
+    }
+  }
+
+  @Override
+  public Savepoint setSavepoint(String name) throws SQLException {
+    checkClosed();
+    try {
+      JdbcSavepoint savepoint = JdbcSavepoint.named(name);
+      getSpannerConnection().savepoint(savepoint.internalGetSavepointName());
+      return savepoint;
+    } catch (SpannerException e) {
+      throw JdbcSqlExceptionFactory.of(e);
+    }
+  }
+
+  @Override
+  public void rollback(Savepoint savepoint) throws SQLException {
+    checkClosed();
+    JdbcPreconditions.checkArgument(savepoint instanceof JdbcSavepoint, savepoint);
+    JdbcSavepoint jdbcSavepoint = (JdbcSavepoint) savepoint;
+    try {
+      getSpannerConnection().rollbackToSavepoint(jdbcSavepoint.internalGetSavepointName());
+    } catch (SpannerException e) {
+      throw JdbcSqlExceptionFactory.of(e);
+    }
+  }
+
+  @Override
+  public void releaseSavepoint(Savepoint savepoint) throws SQLException {
+    checkClosed();
+    JdbcPreconditions.checkArgument(savepoint instanceof JdbcSavepoint, savepoint);
+    JdbcSavepoint jdbcSavepoint = (JdbcSavepoint) savepoint;
+    try {
+      getSpannerConnection().releaseSavepoint(jdbcSavepoint.internalGetSavepointName());
+    } catch (SpannerException e) {
+      throw JdbcSqlExceptionFactory.of(e);
+    }
+  }
+
   @Override
   public Timestamp getCommitTimestamp() throws SQLException {
     checkClosed();
diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcSavepoint.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcSavepoint.java
new file mode 100644
index 0000000..3cd4c41
--- /dev/null
+++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcSavepoint.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.cloud.spanner.jdbc;
+
+import java.sql.SQLException;
+import java.sql.Savepoint;
+import java.util.concurrent.atomic.AtomicInteger;
+
+class JdbcSavepoint implements Savepoint {
+  private static final AtomicInteger COUNTER = new AtomicInteger();
+
+  static JdbcSavepoint named(String name) {
+    return new JdbcSavepoint(-1, name);
+  }
+
+  static JdbcSavepoint unnamed() {
+    int id = COUNTER.incrementAndGet();
+    return new JdbcSavepoint(id, String.format("s_%d", id));
+  }
+
+  private final int id;
+  private final String name;
+
+  private JdbcSavepoint(int id, String name) {
+    this.id = id;
+    this.name = name;
+  }
+
+  @Override
+  public int getSavepointId() throws SQLException {
+    JdbcPreconditions.checkState(this.id >= 0, "This is a named savepoint");
+    return id;
+  }
+
+  @Override
+  public String getSavepointName() throws SQLException {
+    JdbcPreconditions.checkState(this.id < 0, "This is an unnamed savepoint");
+    return name;
+  }
+
+  String internalGetSavepointName() {
+    return name;
+  }
+}


Test patch:
diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcSavepointTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcSavepointTest.java
new file mode 100644
index 0000000..50b000b
--- /dev/null
+++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcSavepointTest.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.cloud.spanner.jdbc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.SQLException;
+import java.sql.Savepoint;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class JdbcSavepointTest {
+
+  @Test
+  public void testNamed() throws SQLException {
+    Savepoint savepoint = JdbcSavepoint.named("test");
+    assertEquals("test", savepoint.getSavepointName());
+    assertThrows(SQLException.class, savepoint::getSavepointId);
+  }
+
+  @Test
+  public void testUnnamed() throws SQLException {
+    Savepoint savepoint = JdbcSavepoint.unnamed();
+    assertTrue(
+        String.format("Savepoint id: %d", savepoint.getSavepointId()),
+        savepoint.getSavepointId() > 0);
+    assertThrows(SQLException.class, savepoint::getSavepointName);
+  }
+}
diff --git a/src/test/java/com/google/cloud/spanner/jdbc/SavepointMockServerTest.java b/src/test/java/com/google/cloud/spanner/jdbc/SavepointMockServerTest.java
new file mode 100644
index 0000000..2c5d518
--- /dev/null
+++ b/src/test/java/com/google/cloud/spanner/jdbc/SavepointMockServerTest.java
@@ -0,0 +1,697 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.cloud.spanner.jdbc;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import com.google.cloud.spanner.Dialect;
+import com.google.cloud.spanner.ErrorCode;
+import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult;
+import com.google.cloud.spanner.Mutation;
+import com.google.cloud.spanner.Statement;
+import com.google.cloud.spanner.connection.AbstractMockServerTest;
+import com.google.cloud.spanner.connection.RandomResultSetGenerator;
+import com.google.cloud.spanner.connection.SavepointSupport;
+import com.google.cloud.spanner.connection.SpannerPool;
+import com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcAbortedDueToConcurrentModificationException;
+import com.google.common.base.Strings;
+import com.google.protobuf.AbstractMessage;
+import com.google.spanner.v1.BeginTransactionRequest;
+import com.google.spanner.v1.CommitRequest;
+import com.google.spanner.v1.ExecuteBatchDmlRequest;
+import com.google.spanner.v1.ExecuteSqlRequest;
+import com.google.spanner.v1.RollbackRequest;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Savepoint;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class SavepointMockServerTest extends AbstractMockServerTest {
+  @Parameter public Dialect dialect;
+
+  @Parameters(name = "dialect = {0}")
+  public static Object[] data() {
+    return Dialect.values();
+  }
+
+  @Before
+  public void setupDialect() {
+    mockSpanner.putStatementResult(StatementResult.detectDialectResult(this.dialect));
+  }
+
+  @After
+  public void clearRequests() {
+    mockSpanner.clearRequests();
+    SpannerPool.closeSpannerPool();
+  }
+
+  private String createUrl() {
+    return String.format(
+        "jdbc:cloudspanner://localhost:%d/projects/%s/instances/%s/databases/%s?usePlainText=true;autoCommit=false",
+        getPort(), "proj", "inst", "db");
+  }
+
+  private Connection createConnection() throws SQLException {
+    return DriverManager.getConnection(createUrl());
+  }
+
+  @Test
+  public void testCreateSavepoint() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection.setSavepoint("s1");
+
+      if (dialect == Dialect.POSTGRESQL) {
+        // PostgreSQL allows multiple savepoints with the same name.
+        connection.setSavepoint("s1");
+      } else {
+        assertThrows(SQLException.class, () -> connection.setSavepoint("s1"));
+      }
+
+      // Test invalid identifiers.
+      assertThrows(SQLException.class, () -> connection.setSavepoint(null));
+      assertThrows(SQLException.class, () -> connection.setSavepoint(""));
+      assertThrows(SQLException.class, () -> connection.setSavepoint("1"));
+      assertThrows(SQLException.class, () -> connection.setSavepoint("-foo"));
+      assertThrows(SQLException.class, () -> connection.setSavepoint(Strings.repeat("t", 129)));
+    }
+  }
+
+  @Test
+  public void testCreateSavepointWhenDisabled() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.DISABLED);
+      assertThrows(SQLException.class, () -> connection.setSavepoint("s1"));
+    }
+  }
+
+  @Test
+  public void testReleaseSavepoint() throws SQLException {
+    try (Connection connection = createConnection()) {
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.releaseSavepoint(s1);
+      assertThrows(SQLException.class, () -> connection.releaseSavepoint(s1));
+
+      Savepoint s1_2 = connection.setSavepoint("s1");
+      Savepoint s2 = connection.setSavepoint("s2");
+      connection.releaseSavepoint(s1_2);
+      // Releasing a savepoint also removes all savepoints after it.
+      assertThrows(SQLException.class, () -> connection.releaseSavepoint(s2));
+
+      if (dialect == Dialect.POSTGRESQL) {
+        // PostgreSQL allows multiple savepoints with the same name.
+        Savepoint savepoint1 = connection.setSavepoint("s1");
+        Savepoint savepoint2 = connection.setSavepoint("s2");
+        Savepoint savepoint1_2 = connection.setSavepoint("s1");
+        connection.releaseSavepoint(savepoint1_2);
+        connection.releaseSavepoint(savepoint2);
+        connection.releaseSavepoint(savepoint1);
+        assertThrows(SQLException.class, () -> connection.releaseSavepoint(savepoint1));
+      }
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepoint() throws SQLException {
+    for (SavepointSupport savepointSupport :
+        new SavepointSupport[] {SavepointSupport.ENABLED, SavepointSupport.FAIL_AFTER_ROLLBACK}) {
+      try (Connection connection = createConnection()) {
+        connection.unwrap(CloudSpannerJdbcConnection.class).setSavepointSupport(savepointSupport);
+
+        Savepoint s1 = connection.setSavepoint("s1");
+        connection.rollback(s1);
+        // Rolling back to a savepoint does not remove it, so we can roll back multiple times to the
+        // same savepoint.
+        connection.rollback(s1);
+
+        Savepoint s2 = connection.setSavepoint("s2");
+        connection.rollback(s1);
+        // Rolling back to a savepoint removes all savepoints after it.
+        assertThrows(SQLException.class, () -> connection.rollback(s2));
+
+        if (dialect == Dialect.POSTGRESQL) {
+          // PostgreSQL allows multiple savepoints with the same name.
+          Savepoint savepoint2 = connection.setSavepoint("s2");
+          Savepoint savepoint1 = connection.setSavepoint("s1");
+          connection.rollback(savepoint1);
+          connection.rollback(savepoint2);
+          connection.rollback(savepoint1);
+          connection.rollback(savepoint1);
+          connection.releaseSavepoint(savepoint1);
+          assertThrows(SQLException.class, () -> connection.rollback(savepoint1));
+        }
+      }
+    }
+  }
+
+  @Test
+  public void testSavepointInAutoCommit() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection.setAutoCommit(true);
+      assertThrows(SQLException.class, () -> connection.setSavepoint("s1"));
+
+      // Starting a 'manual' transaction in autocommit mode should enable savepoints.
+      connection.createStatement().execute("begin transaction");
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.releaseSavepoint(s1);
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointInReadOnlyTransaction() throws SQLException {
+    for (SavepointSupport savepointSupport :
+        new SavepointSupport[] {SavepointSupport.ENABLED, SavepointSupport.FAIL_AFTER_ROLLBACK}) {
+      try (Connection connection = createConnection()) {
+        connection.unwrap(CloudSpannerJdbcConnection.class).setSavepointSupport(savepointSupport);
+        connection.setReadOnly(true);
+
+        // Read-only transactions also support savepoints, but they do not do anything. This feature
+        // is here purely for compatibility.
+        Savepoint s1 = connection.setSavepoint("s1");
+        try (ResultSet resultSet =
+            connection.createStatement().executeQuery(SELECT_RANDOM_STATEMENT.getSql())) {
+          int count = 0;
+          while (resultSet.next()) {
+            count++;
+          }
+          assertEquals(RANDOM_RESULT_SET_ROW_COUNT, count);
+        }
+
+        connection.rollback(s1);
+        try (ResultSet resultSet =
+            connection.createStatement().executeQuery(SELECT_RANDOM_STATEMENT.getSql())) {
+          int count = 0;
+          while (resultSet.next()) {
+            count++;
+          }
+          assertEquals(RANDOM_RESULT_SET_ROW_COUNT, count);
+        }
+        // Committing a read-only transaction is necessary to mark the end of the transaction.
+        // It is a no-op on Cloud Spanner.
+        connection.commit();
+
+        assertEquals(1, mockSpanner.countRequestsOfType(BeginTransactionRequest.class));
+        BeginTransactionRequest beginRequest =
+            mockSpanner.getRequestsOfType(BeginTransactionRequest.class).get(0);
+        assertTrue(beginRequest.getOptions().hasReadOnly());
+        assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
+      }
+      mockSpanner.clearRequests();
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointInReadWriteTransaction() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.ENABLED);
+
+      Savepoint s1 = connection.setSavepoint("s1");
+      try (ResultSet resultSet =
+          connection.createStatement().executeQuery(SELECT_RANDOM_STATEMENT.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(RANDOM_RESULT_SET_ROW_COUNT, count);
+      }
+
+      connection.rollback(s1);
+      try (ResultSet resultSet =
+          connection.createStatement().executeQuery(SELECT_RANDOM_STATEMENT.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(RANDOM_RESULT_SET_ROW_COUNT, count);
+      }
+      connection.commit();
+
+      // Read/write transactions are started with inlined Begin transaction options.
+      assertEquals(0, mockSpanner.countRequestsOfType(BeginTransactionRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+
+      List<AbstractMessage> requests =
+          mockSpanner.getRequests().stream()
+              .filter(
+                  request ->
+                      request instanceof ExecuteSqlRequest
+                          || request instanceof RollbackRequest
+                          || request instanceof CommitRequest)
+              .collect(Collectors.toList());
+      assertEquals(4, requests.size());
+      int index = 0;
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(CommitRequest.class, requests.get(index++).getClass());
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointWithDmlStatements() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.ENABLED);
+
+      // First do a query that is included in the transaction.
+      try (ResultSet resultSet =
+          connection.createStatement().executeQuery(SELECT_RANDOM_STATEMENT.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(RANDOM_RESULT_SET_ROW_COUNT, count);
+      }
+      // Set a savepoint and execute a couple of DML statements.
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      Savepoint s2 = connection.setSavepoint("s2");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      // Rollback the last DML statement and commit.
+      connection.rollback(s2);
+
+      connection.commit();
+
+      // Read/write transactions are started with inlined Begin transaction options.
+      assertEquals(0, mockSpanner.countRequestsOfType(BeginTransactionRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(5, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+
+      List<AbstractMessage> requests =
+          mockSpanner.getRequests().stream()
+              .filter(
+                  request ->
+                      request instanceof ExecuteSqlRequest
+                          || request instanceof RollbackRequest
+                          || request instanceof CommitRequest)
+              .collect(Collectors.toList());
+      assertEquals(7, requests.size());
+      int index = 0;
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(CommitRequest.class, requests.get(index++).getClass());
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointFails() throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.ENABLED);
+
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+      // Set a savepoint and execute a couple of DML statements.
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      // Change the result of the initial query.
+      mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+      // Rollback to before the DML statements.
+      // This will succeed as long as we don't execute any further statements.
+      connection.rollback(s1);
+
+      // Trying to commit the transaction or execute any other statements on the transaction will
+      // fail.
+      assertThrows(JdbcAbortedDueToConcurrentModificationException.class, connection::commit);
+
+      // Read/write transactions are started with inlined Begin transaction options.
+      assertEquals(0, mockSpanner.countRequestsOfType(BeginTransactionRequest.class));
+      assertEquals(2, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(4, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+
+      List<AbstractMessage> requests =
+          mockSpanner.getRequests().stream()
+              .filter(
+                  request ->
+                      request instanceof ExecuteSqlRequest
+                          || request instanceof RollbackRequest
+                          || request instanceof CommitRequest)
+              .collect(Collectors.toList());
+      assertEquals(6, requests.size());
+      int index = 0;
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointWithFailAfterRollback() throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.FAIL_AFTER_ROLLBACK);
+
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+      // Set a savepoint and execute a couple of DML statements.
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      // Rollback to before the DML statements.
+      // This will succeed as long as we don't execute any further statements.
+      connection.rollback(s1);
+
+      // Trying to commit the transaction or execute any other statements on the transaction will
+      // fail with an FAILED_PRECONDITION error, as using a transaction after a rollback to
+      // savepoint has been disabled.
+      SQLException exception = assertThrows(SQLException.class, connection::commit);
+      assertEquals(
+          ErrorCode.FAILED_PRECONDITION.getGrpcStatusCode().value(), exception.getErrorCode());
+      assertEquals(
+          "FAILED_PRECONDITION: Using a read/write transaction after rolling back to a "
+              + "savepoint is not supported with SavepointSupport=FAIL_AFTER_ROLLBACK",
+          exception.getMessage());
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointSucceedsWithRollback() throws SQLException {
+    for (SavepointSupport savepointSupport :
+        new SavepointSupport[] {SavepointSupport.ENABLED, SavepointSupport.FAIL_AFTER_ROLLBACK}) {
+      Statement statement = Statement.of("select * from foo where bar=true");
+      int numRows = 10;
+      RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+      mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+      try (Connection connection = createConnection()) {
+        connection.unwrap(CloudSpannerJdbcConnection.class).setSavepointSupport(savepointSupport);
+
+        try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+          int count = 0;
+          while (resultSet.next()) {
+            count++;
+          }
+          assertEquals(numRows, count);
+        }
+        // Change the result of the initial query and set a savepoint.
+        Savepoint s1 = connection.setSavepoint("s1");
+        mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+        // This will succeed as long as we don't execute any further statements.
+        connection.rollback(s1);
+
+        // Rolling back the transaction should now be a no-op, as it has already been rolled back.
+        connection.rollback();
+
+        // Read/write transactions are started with inlined Begin transaction options.
+        assertEquals(0, mockSpanner.countRequestsOfType(BeginTransactionRequest.class));
+        assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+        assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
+        assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+      }
+      mockSpanner.clearRequests();
+    }
+  }
+
+  @Test
+  public void testMultipleRollbacksWithChangedResults() throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      Savepoint s2 = connection.setSavepoint("s2");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+
+      // Change the result of the initial query to make sure that any retry will fail.
+      mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+      // This will succeed as long as we don't execute any further statements.
+      connection.rollback(s2);
+      // Rolling back one further should also work.
+      connection.rollback(s1);
+
+      // Rolling back the transaction should now be a no-op, as it has already been rolled back.
+      connection.rollback();
+
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(0, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(3, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+    }
+  }
+
+  @Test
+  public void testMultipleRollbacks() throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.ENABLED);
+
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      Savepoint s2 = connection.setSavepoint("s2");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+
+      // First roll back one step and then one more.
+      connection.rollback(s2);
+      connection.rollback(s1);
+
+      // This will only commit the SELECT query.
+      connection.commit();
+
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(4, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+
+      List<AbstractMessage> requests =
+          mockSpanner.getRequests().stream()
+              .filter(
+                  request ->
+                      request instanceof ExecuteSqlRequest
+                          || request instanceof RollbackRequest
+                          || request instanceof CommitRequest)
+              .collect(Collectors.toList());
+      assertEquals(6, requests.size());
+      int index = 0;
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(CommitRequest.class, requests.get(index++).getClass());
+    }
+  }
+
+  @Test
+  public void testRollbackMutations() throws SQLException {
+    try (Connection con = createConnection()) {
+      CloudSpannerJdbcConnection connection = con.unwrap(CloudSpannerJdbcConnection.class);
+      connection.setSavepointSupport(SavepointSupport.ENABLED);
+
+      connection.bufferedWrite(Mutation.newInsertBuilder("foo1").build());
+      Savepoint s1 = connection.setSavepoint("s1");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      connection.bufferedWrite(Mutation.newInsertBuilder("foo2").build());
+      connection.setSavepoint("s2");
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      connection.bufferedWrite(Mutation.newInsertBuilder("foo3").build());
+
+      connection.rollback(s1);
+
+      // This will only commit the first mutation.
+      connection.commit();
+
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(2, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+      CommitRequest commitRequest = mockSpanner.getRequestsOfType(CommitRequest.class).get(0);
+      assertEquals(1, commitRequest.getMutationsCount());
+      assertEquals("foo1", commitRequest.getMutations(0).getInsert().getTable());
+    }
+  }
+
+  @Test
+  public void testRollbackBatchDml() throws SQLException {
+    try (Connection connection = createConnection()) {
+      connection
+          .unwrap(CloudSpannerJdbcConnection.class)
+          .setSavepointSupport(SavepointSupport.ENABLED);
+
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      Savepoint s1 = connection.setSavepoint("s1");
+      try (java.sql.Statement statement = connection.createStatement()) {
+        statement.addBatch(INSERT_STATEMENT.getSql());
+        statement.addBatch(INSERT_STATEMENT.getSql());
+        statement.executeBatch();
+      }
+      Savepoint s2 = connection.setSavepoint("s2");
+
+      connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql());
+      Savepoint s3 = connection.setSavepoint("s3");
+      try (java.sql.Statement statement = connection.createStatement()) {
+        statement.addBatch(INSERT_STATEMENT.getSql());
+        statement.addBatch(INSERT_STATEMENT.getSql());
+        statement.executeBatch();
+      }
+      connection.setSavepoint("s4");
+
+      connection.rollback(s2);
+
+      connection.commit();
+
+      assertEquals(1, mockSpanner.countRequestsOfType(RollbackRequest.class));
+      assertEquals(1, mockSpanner.countRequestsOfType(CommitRequest.class));
+      assertEquals(3, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
+      assertEquals(3, mockSpanner.countRequestsOfType(ExecuteBatchDmlRequest.class));
+
+      List<AbstractMessage> requests =
+          mockSpanner.getRequests().stream()
+              .filter(
+                  request ->
+                      request instanceof ExecuteSqlRequest
+                          || request instanceof RollbackRequest
+                          || request instanceof CommitRequest
+                          || request instanceof ExecuteBatchDmlRequest)
+              .collect(Collectors.toList());
+      assertEquals(8, requests.size());
+      int index = 0;
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteBatchDmlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteBatchDmlRequest.class, requests.get(index++).getClass());
+      assertEquals(RollbackRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteSqlRequest.class, requests.get(index++).getClass());
+      assertEquals(ExecuteBatchDmlRequest.class, requests.get(index++).getClass());
+      assertEquals(CommitRequest.class, requests.get(index++).getClass());
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointWithoutInternalRetries() throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      connection.unwrap(CloudSpannerJdbcConnection.class).setRetryAbortsInternally(false);
+
+      Savepoint s1 = connection.setSavepoint("s1");
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+      // This should work.
+      connection.rollback(s1);
+      // Resuming after a rollback is not supported without internal retries enabled.
+      assertThrows(
+          SQLException.class,
+          () -> connection.createStatement().executeUpdate(INSERT_STATEMENT.getSql()));
+    }
+  }
+
+  @Test
+  public void testRollbackToSavepointWithoutInternalRetriesInReadOnlyTransaction()
+      throws SQLException {
+    Statement statement = Statement.of("select * from foo where bar=true");
+    int numRows = 10;
+    RandomResultSetGenerator generator = new RandomResultSetGenerator(numRows);
+    mockSpanner.putStatementResult(StatementResult.query(statement, generator.generate()));
+    try (Connection connection = createConnection()) {
+      connection.unwrap(CloudSpannerJdbcConnection.class).setRetryAbortsInternally(false);
+      connection.setReadOnly(true);
+
+      Savepoint s1 = connection.setSavepoint("s1");
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+
+      // Both rolling back and resuming after a rollback are supported in a read-only transaction,
+      // even if internal retries have been disabled.
+      connection.rollback(s1);
+      try (ResultSet resultSet = connection.createStatement().executeQuery(statement.getSql())) {
+        int count = 0;
+        while (resultSet.next()) {
+          count++;
+        }
+        assertEquals(numRows, count);
+      }
+    }
+  }
+}