Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to commit/rollback SqlTransaction in asynchronous?

Tags:

c#

sql-server

I'm trying to commit/rollback SqlTransaction in asynchronous. But it look like asynchronous is not supported. Is there any way to make it asynchronous without using raw SQL to start transaction?

like image 418
UltimaWeapon Avatar asked Jul 01 '15 05:07

UltimaWeapon


People also ask

Is rollback possible after commit?

After you commit the transaction, the changes are visible to other users' statements that execute after the commit. You can roll back (undo) any changes made during the transaction with the ROLLBACK statement (see ROLLBACK.

Can we rollback committed transaction in SQL?

Once SQL Server commits a transaction, you cannot run the ROLLBACK statement.

Can we rollback without commit?

On ending the transaction without specifying committing or rolling back, it will roll back. So, the advice given below is definitely correct: always explicitly commit or rollback.

Can we execute commit immediately after rollback?

I have confirmed that after rollback we cannot commit the same transaction. Make sure another transaction is not in waiting, else it will be committed.


1 Answers

With .Net Core 3.0, it is now theoretically doable to commit or rollback asynchronously a transaction, with any transaction deriving from DbTransaction. So with SqlTransaction too.

See .Net Core issue #35012. (Or DbTransaction documentation.)

But more important for your concern, SqlTransaction underlying implementation does not leverage it yet: you can call async methods on it, but currently as far as I can see in the source code, they are still delegating to their sync counterparts through DbTransaction default implementation. And this is the case also with Microsoft.Data.SqlClient (no async overrides).

So you can get your code ready to commit or rollback asynchronously with .Net Core 3.0, but you will have to wait a bit more to get them actually async.

like image 114
Frédéric Avatar answered Nov 15 '22 16:11

Frédéric