Repodb: CancellationToken support?

Created on 17 Nov 2019  路  22Comments  路  Source: mikependon/RepoDB

Hey ya, do you have any plans to support CancellationToken's?

Fixed deployed question todo

Most helpful comment

I have not seen use of undo for synchronous operations when working with DbCommand...

All 22 comments

Hi @jakejscott , actually no. But, I really would like to if you can share me the detailed explanation and which scenario. Can you expand this thread to let me understand it further?

@mikependon why not? This is necessary to cancel long-running operations (like batch insert). There must be a way to stop an operation that is no longer needed. Or you will have to wait for its end.

TBH, I know this is quite needed in some cases, but such request has not been triggered after this. The user closes the request ticket without providing enough information. Happy to reopen this if you can provide same samples, scenarios and/or recommendations.

Also, I think I need to spend time researching what happened to the DB Provider side if the operation has been executed already, but a cancel has been requested.

This could be related to this dotnet/runtime story.

@jakejscott @SergerGood - I can easily add the CancellationToken on the Async methods, however, how would you like to handle the sync version?

Remember, you do not have access to the DbCommand object in all kinds of execution within RepoDB, therefore, you cannot call the DbCommand.Cancel() method. Of course, unless we place it to the ClassHandler (#530) object and/or provide a callback function calls.

What's your preference?

It will be like this for example https://github.com/mikependon/RepoDB/commit/558008863e0ded13c0e0fe3d68cde3a46f77860a
And the use is like this

        public async Task<List<Person>> Query(CancellationToken cancellationToken)
        {
            using IDbConnection connection = await new SqlConnection(ConnectionString)
                .EnsureOpenAsync(cancellationToken);

            return await connection.QueryAsync<Person>(x => x.Id == CurrentId, cancellationToken: cancellationToken);
        }

@SergerGood - I am actually doing this one already, but it is only for Async. How about the sync version? How would you do that as we do not have reference to the DbCommand.Cancel().

I have not seen use of undo for synchronous operations when working with DbCommand...

May i ask why not used default?

I think to force it to be nullable instead for the reason of overloaded methods. There might be case that the user will never ever pass a value to it, and placing a default would force the execution to an overloaded method of the underlying DbCommand.

I used to do it this way.

using (var reader = cancellationToken.HasValue ? await command.ExecuteReaderAsync(cancellationToken.Value) : await command.ExecuteReaderAsync())
{
     ...
}

And by having a default, would force it to the await command.ExecuteReaderAsync(cancellationToken), in which the value of the cancellationToken is the CancellationToken.None. I guess it should not be like that?

CancellationToken.None is the default way to pass empty CancellationToken.
If CancellationToken.None used then that is, its CanBeCanceled property is false. https://github.com/microsoft/referencesource/blob/f461f1986ca4027720656a0c77bede9963e20b7e/System.Data/System/Data/Common/DBCommand.cs#L250 The user does not need to pass a value by default, and the behavior is handled correctly.

Yeah, in such a case, you can always pass it on the nullabe argument right? The behavior is to make sure not affecting as well all the previous implementations.

If used as a parameter with a default value, it has no effect for all the previous implementations.

..., CancellationToken cancellationToken = default)

@SergerGood - you are correct on pushing this, they are passing the default value here. Great sleuthing!

Relating the recent found bugs at #601.

Linking this changeset (https://github.com/mikependon/RepoDB/commit/62cac1ff4b25abe613d3888c002aac5ede2d8b62) in relation to the CancellationToken Support for the Exists Operation.

Linking this changeset (62cac1f) in relation to the CancellationToken Support for the Insert Operation.

@SergerGood - I have to remove the explicit calls you made for cancellationToken.ThrowIfCancellationRequested();. Let us just pass it and have RepoDB bubble ups the exception thrown by ADO.NET itself. Hope it is okay.

@mikependon - I think that it is ok)

@SergerGood - I am done with the changes, but we have some failing Unit and Integration Tests. Will work on this soon. In the meanwhile, would you be able to do a double check while I am fixing the failing tests? Thanks

This is now available at RepoDB v1.12.4. Please see the actual release here.

Setting this one to close now.

Was this page helpful?
0 / 5 - 0 ratings