Skip to content

doc: add MutableCredentials Example#4382

Draft
ldetmer wants to merge 4 commits intomainfrom
mutable-cred-ex
Draft

doc: add MutableCredentials Example#4382
ldetmer wants to merge 4 commits intomainfrom
mutable-cred-ex

Conversation

@ldetmer
Copy link
Collaborator

@ldetmer ldetmer commented Mar 10, 2026

This adds an example of how to use MutableCredentials to update credentials on a long running client if the timestamp on a service account credentials file has changed.

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: spanner Issues related to the googleapis/java-spanner API. labels Mar 10, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Google Cloud Spanner client library examples by introducing a new demonstration of MutableCredentials. This example provides a practical guide for developers on how to implement dynamic credential rotation for long-running applications, ensuring continuous and secure access to Spanner without requiring client reinitialization. The change also includes a minor version bump for the Spanner client library dependency.

Highlights

  • New Example Added: A new example, MutableCredentialsExample.java, was added to demonstrate how to use MutableCredentials to update service account credentials dynamically for long-running Google Cloud Spanner clients.
  • Dependency Version Update: The google-cloud-spanner dependency version was updated from 6.110.0 to 6.111.1 in both README.md and samples/install-without-bom/pom.xml.
  • Documentation Update: The README.md file was updated to include the newly added Mutable Credentials Example in the list of available samples.
Changelog
  • README.md
    • Updated the google-cloud-spanner dependency version to 6.111.1.
    • Added a new entry for the MutableCredentials Example to the samples table.
  • samples/install-without-bom/pom.xml
    • Updated the google-cloud-spanner dependency version to 6.111.1.
  • samples/snippets/src/main/java/com/example/spanner/MutableCredentialsExample.java
    • Added a new Java example file demonstrating how to use MutableCredentials to periodically check and update service account credentials from a file.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a helpful example for using MutableCredentials to handle credential rotation for long-running Spanner clients. The example is clear and demonstrates the key steps involved.

I've added a few review comments to improve the example:

  • A suggestion to use AtomicReference for better code clarity.
  • A critical fix to make the example correctly simulate a long-running process, as it currently shuts down immediately.

Additionally, it would be beneficial to add an integration test for this new example in SpannerStandaloneExamplesIT.java to ensure it runs correctly and prevent future regressions.

Comment on lines +87 to +95
try (Spanner spanner = options.getService();
DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
// Perform operations...
// long running client operations will always use the latest credentials wrapped in
// mutableCredentials
} finally {
// Ensure the executor is shut down when the application exits or the client is closed
executorService.shutdown();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This example is intended to demonstrate credential updates for a long-running client, but the Spanner client and the ScheduledExecutorService are shut down immediately after creation because the try-with-resources block is empty. This prevents the credential rotation logic from ever running in a meaningful way.

To make the example more illustrative, consider adding a TimeUnit.sleep() within the try block to simulate a long-running process. This will keep the client and the background thread alive long enough to demonstrate the credential update mechanism.

    // 5. Use the client
    try (Spanner spanner = options.getService();
        DatabaseAdminClient databaseAdminClient = spanner.createDatabaseAdminClient()) {
      // Perform operations...
      // long running client operations will always use the latest credentials wrapped in
      // mutableCredentials.
      // This example demonstrates this by keeping the client alive for 60 seconds.
      System.out.println("Client created. It will remain active for 60 seconds.");
      try {
        TimeUnit.SECONDS.sleep(60);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
      }
      System.out.println("60 seconds passed. Closing client.");
    } finally {
      // Ensure the executor is shut down when the application exits or the client is closed
      executorService.shutdown();
    }

Comment on lines +42 to +43
// Use an array to hold the mutable lastModifiedTime so it can be accessed in the lambda
FileTime[] lastModifiedTime = new FileTime[] {Files.getLastModifiedTime(path)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better clarity and to use modern Java concurrency utilities, consider using java.util.concurrent.atomic.AtomicReference instead of a single-element array to hold a mutable reference. This makes the intent of sharing a mutable object with the lambda more explicit. This change should be applied along with an update to the lambda that uses this variable.

Suggested change
// Use an array to hold the mutable lastModifiedTime so it can be accessed in the lambda
FileTime[] lastModifiedTime = new FileTime[] {Files.getLastModifiedTime(path)};
// Use an AtomicReference to hold the mutable lastModifiedTime so it can be accessed in the lambda
final java.util.concurrent.atomic.AtomicReference<FileTime> lastModifiedTime =
new java.util.concurrent.atomic.AtomicReference<>(Files.getLastModifiedTime(path));

Comment on lines +71 to +72
if (currentModifiedTime.compareTo(lastModifiedTime[0]) > 0) {
lastModifiedTime[0] = currentModifiedTime;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

As part of refactoring to use AtomicReference for lastModifiedTime, you'll need to update this code to use get() and set() to access and modify the value.

Suggested change
if (currentModifiedTime.compareTo(lastModifiedTime[0]) > 0) {
lastModifiedTime[0] = currentModifiedTime;
if (currentModifiedTime.compareTo(lastModifiedTime.get()) > 0) {
lastModifiedTime.set(currentModifiedTime);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the googleapis/java-spanner API. size: m Pull request size is medium.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants