Skip to content

fix(spanner): validate connection before Prepare - #16048

Open
alaricd wants to merge 1 commit into
googleapis:mainfrom
alaricd:codex/spanner-prepare-validation
Open

alaricd wants to merge 1 commit into
googleapis:mainfrom
alaricd:codex/spanner-prepare-validation

Conversation

@alaricd

@alaricd alaricd commented Oct 29, 2026

Copy link
Copy Markdown

Summary

Make SpannerCommand.Prepare throw InvalidOperationException when no connection is assigned or when its connection is closed. Keep preparation a no-op when the connection is open, as Spanner does not support precompiled queries. Add regression tests for both invalid states.

Resolves #16047.

Microsoft contract

Microsoft documents InvalidOperationException when IDbCommand.Prepare is called without a connection or with a closed connection: https://learn.microsoft.com/dotnet/api/system.data.idbcommand.prepare

Compatibility

Commands with an open connection retain the existing no-op preparation behavior. Calls that currently succeed without an open connection will now throw as documented; applications relying on preparing disconnected commands may observe this change.

Validation

Focused provider tests: dotnet test apis/Google.Cloud.Spanner.Data/Google.Cloud.Spanner.Data.Tests/Google.Cloud.Spanner.Data.Tests.csproj --no-restore -f net8.0 -p:NoWarn=CS8981%3BxUnit2004%3BxUnit2013 --filter FullyQualifiedName~Prepare — 2 passed, 0 failed.

Discovery

Found using pengdows.AdoNetApiTest.conformance.

@alaricd
alaricd requested a review from a team as a code owner October 29, 2026 07:10
@google-cla

google-cla Bot commented Oct 29, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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

Copy link
Copy Markdown
Contributor

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 updates the Prepare method in SpannerCommand to throw an InvalidOperationException if the connection is unset or not open, and adds corresponding unit tests. The review feedback suggests caching the SpannerConnection property in a local variable before checking its state to ensure thread safety and avoid potential compiler warnings.

Comment on lines +455 to +462
if (SpannerConnection == null)
{
throw new InvalidOperationException("The Connection property has not been set.");
}
if (SpannerConnection.State != ConnectionState.Open)
{
throw new InvalidOperationException("The connection is not open.");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure thread safety and avoid potential compiler warnings (such as CS8602) when nullable reference types are enabled, it is recommended to cache the mutable SpannerConnection property in a local variable before performing the null check and accessing its State property.

            var connection = SpannerConnection;
            if (connection == null)
            {
                throw new InvalidOperationException("The Connection property has not been set.");
            }
            if (connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("The connection is not open.");
            }

@alaricd
alaricd force-pushed the codex/spanner-prepare-validation branch from 686dda4 to b5ce074 Compare October 29, 2026 07:25
@alaricd alaricd changed the title Validate connection state in SpannerCommand.Prepare fix(spanner): validate connection before Prepare Oct 29, 2026
@alaricd
alaricd force-pushed the codex/spanner-prepare-validation branch from b5ce074 to bb3758d Compare October 29, 2026 07:34
@product-auto-label product-auto-label Bot added the api: spanner Issues related to the Spanner API. label Oct 29, 2026
@amanda-tarafa
amanda-tarafa marked this pull request as draft October 29, 2026 16:27
@amanda-tarafa amanda-tarafa self-assigned this Oct 29, 2026
@amanda-tarafa
amanda-tarafa self-requested a review October 29, 2026 16:28
@alaricd
alaricd marked this pull request as ready for review October 29, 2026 18:41

This branch has not been deployed

No deployments
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 Spanner API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpannerCommand.Prepare should reject a missing or closed connection

2 participants