What is the benefit to wrapping every sql/stored proc invocation in a transaction?

Posted by MatthewMartin on Stack Overflow See other posts from Stack Overflow or by MatthewMartin
Published on 2010-05-26T16:26:56Z Indexed on 2010/05/26 16:31 UTC
Read the original article Hit count: 173

Filed under:
|
|
|

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

In the sample code below, if the delete fails, it fails. There is nothing else to be rolled back (it seems). So why is everything wrapped in a transaction anyhow?

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;
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about sql