What is the use of packing every sql / stored proc call in a transaction?

The following code executes one stored procedure. A stored procedure contains only one command. Is there any use to wrapping everything into a transaction, even if it has only one SQL statement (or one stored proc that has only one sql statement)?

In the following code example, if the deletion failed, it fails. Nothing more to roll back (it seems). So why is everything in a transaction?

using (ITransactionManager transMan = repository.TransactionManager())
using (IController controller = repository.Controller())
{
    transMan.BeginTransaction();
    try
    {
        //DELETE FROM myTable where Id=@id
        controller.Delete(id);
        transMan.CommitTransaction();
    }
    catch
    {
        transMan.RollbackTransaction();
        throw;
    }
}
+3
source share
3 answers

, ( ), -, .

, , , .

, - , .

.

+5

. , , , .

, , , , . , .

+2

Even if you do not explicitly start the transaction, all database queries are executed in an implicit transaction. Check out this tip from the NHProf homepage . It describes very well why you should almost always use an explicit transaction.

+2
source

Source: https://habr.com/ru/post/1747223/


All Articles