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?
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.
Once SQL Server commits a transaction, you cannot run the ROLLBACK statement.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With