ydb-platform / ydb-dotnet-sdk

YDB .NET SDK
Apache License 2.0
31 stars 17 forks source link

bug: Transaction with more than 2 queries is always committed after second query #69

Closed LipatovAlexander closed 7 months ago

LipatovAlexander commented 7 months ago

Bug Report

YDB dotnet SDK version: 0.2.0

Environment: Yandex Cloud Function, dotnet8

Current behavior:

Firstly, I faced the problem with logger. See https://github.com/ydb-platform/ydb-dotnet-sdk/blob/main/src/Ydb.Sdk/src/Services/Table/ExecuteDataQuery.cs#L68 and https://github.com/ydb-platform/ydb-dotnet-sdk/blob/main/src/Ydb.Sdk/src/Services/Table/Transaction.cs#L121. logger.LogTrace($"Using tx #{_txNum}"); throws ArgumentNullException. I fixed this and published new package, but it still doesn't work.

Now it seems that transaction is always committed after second query, even without calling .Commit() in TxControl.Tx(..) in second query.

Expected behavior:

I can do multiple (more than two) queries in one transaction using TxControl.

Steps to reproduce:

Add an intermediate query to this example. Like this:

var execResponse = await Client.SessionExec(async session =>
            {
                var query1 = "<some_query>";

                // Execute first query (no transaction commit)
                var response = await session.ExecuteDataQuery(
                    query: query1,
                    txControl: TxControl.BeginSerializableRW(),
                    parameters: new Dictionary<string, YdbValue>()
                );

                if (!response.Status.IsSuccess || response.Tx is null)
                {
                    return response;
                }

                var query2 = "<some_query>";

                // Execute second query (no transaction commit)
                response = await session.ExecuteDataQuery(
                    query: query2,
                    TxControl.Tx(response.Tx),
                    parameters: new Dictionary<string, YdbValue>()
                );

                # here transaction is committed and response.Tx is null 

                if (!response.Status.IsSuccess || response.Tx is null)
                {
                    return response;
                }

                var query3 = "<some_query>";

                // Execute third query and commit transaction
                response = await session.ExecuteDataQuery(
                    query: query3,
                    TxControl.Tx(response.Tx).Commit(),
                    parameters: new Dictionary<string, YdbValue>()
                );

                return response;
            });

            execResponse.Status.EnsureSuccess();

Remark: I use TableClient here. I tried to use QueryClient, but it doesn't work for me too. There was some error about endpoints (not related to this issue and transactions).

LipatovAlexander commented 7 months ago

Ah, I found the problem in my code. I am closing this issue and will open PR with logger ArgumentNullException fix

LipatovAlexander commented 7 months ago

https://github.com/ydb-platform/ydb-dotnet-sdk/pull/70