Cloud spanner: Read write transaction retries to be off

Hi There, I'm require to switch off the retries in readWriteTransaction query block. Can anyone please help me out?

Our application is in Java (Spring boot), I have tried this using the retry settings while creating the DB client but that doesn't work out for me.

Thanks in Advance.

0 5 785
5 REPLIES 5

To switch off retries in readWriteTransaction query block in Google Cloud Spanner Java Spring Boot application, you need to customize the RetrySettings:

  1. Create a SpannerOptions object with custom RetrySettings.
  2. Use the SpannerOptions object to create a Spanner service object.
  3. Use the Spanner service object to get a DatabaseClient.
  4. Use the DatabaseClient to run a readWriteTransaction.

import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.DatabaseClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.api.gax.retrying.RetrySettings;
import org.threeten.bp.Duration;

// Customize RetrySettings to switch off retries.
RetrySettings retrySettings = RetrySettings.newBuilder()
    .setMaxAttempts(1) // This ensures no retries.
    .build();

// Create SpannerOptions with the custom RetrySettings.
SpannerOptions options = SpannerOptions.newBuilder()
    .setRetrySettings(retrySettings)
    .build();

// Create a Spanner service object.
Spanner spanner = options.getService();

// Get the DatabaseClient.
DatabaseId db = DatabaseId.of("your-project-id", "your-instance-id", "your-database-id");
DatabaseClient dbClient = spanner.getDatabaseClient(db);

// Use the DatabaseClient to run a readWriteTransaction.
dbClient.readWriteTransaction().run(transaction -> {
  // Your transaction logic here.
  return null;
});

If you've tried the above and it's still not working, ensure that there aren't other parts of your code that override these settings. Additionally, make sure that the Spring Boot auto-configuration isn't interfering with your custom settings. If you're using Spring Cloud GCP, you might need to customize the Spanner bean configuration.

 

I have tried the same way to switch off the retry but still I see the retry happening every time. I didn't create any other configuration/bean for this which can cause the overriding issue.

Not sure why its happening.

Retries may still occur in Spanner even if you've attempted to disable them in your code. Here are potential reasons:

  1. Spanner Client Library Bug: There could be a bug in the Spanner client library causing unintended retries. If you're using an older version of the library, consider updating as the issue might have been addressed in newer releases.

  2. Spring Boot Bean Configuration: In a Spring Boot application, it's possible that a bean configuration is overriding the retry settings you've set.

  3. External Configuration: Another configuration, such as in spanner_grpc_service_config.json or spanner_admin_instance_grpc_service_config.json, might be overriding your retry settings. These files contain configuration settings for Spanner's gRPC connections.

Steps to Troubleshoot:

  • Update the Spanner client library to the latest version.

  • Examine your Spring Boot application's code to identify any beans that might be overriding the retry settings.

  • Review any other Spanner-related configuration files to check for settings that could be affecting retries.

Additional Troubleshooting Tips:

  • Utilize the Google Cloud Logging service to inspect logs for your Spanner instance. This can help pinpoint specific errors triggering the retries.

  • Employ standard Java debugging tools and practices to delve into your Spanner application, identifying any code segments responsible for the retries.

  • Think about using the Spanner emulator for local application testing. This can help discern if the issues are related to the Spanner service itself or your application's configuration.

Thanks for giving a detailed explanation here. Though I have already did all this -

  • No overriding settings are there in the application.
  • No external config is there.
  • May be the bug of client library but I am already using the latest one.

Still I am not able to solve this retry issue (disable the retries in readWriteTransaction block) but trying to fix the concurrency issue by some other way. Thanks.

I understand your frustration. Troubleshooting issues like this can be challenging, especially when you've already taken the recommended steps.

One approach to consider is using the Spanner emulator to run your application locally. This can help determine if the issue originates from the Spanner service or your application's configuration.

To use the Spanner emulator:

  1. Install the Google Cloud SDK and the Cloud Spanner Emulator. Detailed instructions can be found in the official documentation
  2. Start the emulator using the command:
gcloud beta emulators spanner start
  1. Set the SPANNER_EMULATOR_HOST environment variable to point to the emulator's endpoint:
export SPANNER_EMULATOR_HOST=localhost:9010
  1. Ensure that no other configurations in your application properties or code override this setting.
  2. Run your application and attempt to reproduce the issue. If the problem doesn't manifest when using the emulator, it's likely an issue with the Spanner service. If it does, you can delve deeper into your application to pinpoint the cause.

Additionally, you might consider experimenting with a different version of the Spanner client library to see if the issue persists. However, be cautious when switching versions, as older versions might lack certain features or have other limitations.

If the issue remains unresolved, reaching out to Google Cloud support might be beneficial.