Skip to content

fix(spanner): clear transaction connection after completion - #16052

Open
alaricd wants to merge 1 commit into
googleapis:mainfrom
alaricd:codex/spanner-transaction-connection-state
Open

alaricd wants to merge 1 commit into
googleapis:mainfrom
alaricd:codex/spanner-transaction-connection-state

Conversation

@alaricd

@alaricd alaricd commented Oct 29, 2026

Copy link
Copy Markdown

Summary

Return null from the inherited ADO.NET DbTransaction.Connection property after the Spanner transaction has been disposed or otherwise invalidated, while preserving the provider-specific SpannerConnection reference. Add a regression test for disposal.

Resolves #16051.

Microsoft contract

Microsoft documents that IDbTransaction.Connection returns null when the transaction is no longer valid: https://learn.microsoft.com/dotnet/api/system.data.common.dbtransaction.system-data-idbtransaction-connection

Compatibility

The inherited ADO.NET Connection surface will change from the original connection to null after transaction invalidation. The provider-specific SpannerConnection property remains available. Consumers using DbTransaction.Connection after completion will observe the documented invalid-state value.

Validation

Focused provider test: 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~Connection_IsNullAfterDispose — 1 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:13
@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 SpannerTransaction to return null for its DbConnection property once the transaction is disposed, and adds a unit test to verify this behavior. The review feedback correctly points out that according to the ADO.NET specification, the connection should also return null after the transaction has been committed or rolled back, not just upon disposal. It is recommended to update the implementation to handle these additional states and expand the test coverage accordingly.


/// <inheritdoc />
protected override DbConnection DbConnection => SpannerConnection;
protected override DbConnection DbConnection => _disposed == 0 ? SpannerConnection : null;

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

According to the ADO.NET specification, DbTransaction.Connection should return null when the transaction is no longer valid, which includes after it has been committed or rolled back, not just when it is disposed. If _disposed is only set during disposal, DbConnection will still return the connection after Commit() or Rollback() until Dispose() is called. Please check if there is an existing state or completion flag that can be used to return null after commit or rollback, and add corresponding tests for these scenarios.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed: transaction completion is tracked so Connection returns null after commit, rollback, or disposal, with regression coverage.

Comment on lines +51 to +63
public void Connection_IsNullAfterDispose()
{
SpannerClient spannerClientMock = SpannerClientHelpers.CreateMockClient(Logger.DefaultLogger);
spannerClientMock.SetupCreateSessionAsync();
SpannerConnection connection = SpannerCommandTests.BuildSpannerConnection(spannerClientMock);
SpannerTransaction transaction = connection.BeginTransaction();

Assert.Same(connection, transaction.Connection);

transaction.Dispose();

Assert.Null(transaction.Connection);
}

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 full compliance with the ADO.NET specification, consider adding test cases to verify that Connection also returns null after the transaction is committed or rolled back, as the transaction is no longer valid after these operations.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added regression coverage for Connection returning null after commit and rollback.

@alaricd
alaricd force-pushed the codex/spanner-transaction-connection-state branch from 2ff2cd5 to 5d579d2 Compare October 29, 2026 07:25
@alaricd alaricd changed the title Clear DbTransaction.Connection after disposal fix(spanner): clear transaction connection after disposal Oct 29, 2026
@alaricd
alaricd force-pushed the codex/spanner-transaction-connection-state branch from 5d579d2 to e445907 Compare October 29, 2026 07:34
@alaricd
alaricd force-pushed the codex/spanner-transaction-connection-state branch from e445907 to 70a31f4 Compare October 29, 2026 07:37
@alaricd alaricd changed the title fix(spanner): clear transaction connection after disposal fix(spanner): clear transaction connection after completion Oct 29, 2026
@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:26
@amanda-tarafa amanda-tarafa self-assigned this Oct 29, 2026
@amanda-tarafa
amanda-tarafa self-requested a review October 29, 2026 16:26
@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.

SpannerTransaction.Connection should be null after transaction completion

2 participants