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.
To switch off retries in readWriteTransaction
query block in Google Cloud Spanner Java Spring Boot application, you need to customize the RetrySettings
:
SpannerOptions
object with custom RetrySettings
.SpannerOptions
object to create a Spanner
service object.Spanner
service object to get a DatabaseClient
.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;
});
maxAttempts
to 1
, the transaction will not be retried, so if it fails due to any transient error, you'll need to handle it manually in your application logic.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:
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.
Spring Boot Bean Configuration: In a Spring Boot application, it's possible that a bean configuration is overriding the retry settings you've set.
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 -
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:
gcloud beta emulators spanner start
export SPANNER_EMULATOR_HOST=localhost:9010
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.