Reputation: 2105
When my C#.net application updates records in more than one table, I use transactions so if anything should fail during the transaction I can rollback.
Which one is a better practice?
-Use stored procedure with BEGIN TRANSACTION/ROLLBACK/COMMIT TRANSACTION; -Use TransactionScope in the application as below:
using (TransactionScope ts = new TransactionScope())
{
}
Upvotes: 8
Views: 2801
Reputation: 103579
Here are my 2 simple rules for when to use transactions:
Upvotes: 1
Reputation: 2094
I would strongly recommend setting up one procedure for a page and managing all sql actions there. If there are multiple tasks to perform on the page that require multiple procedures, just have one procedure manage the other procedures. Your proceedure can always return multiple recordsets if needed.
Upvotes: 1
Reputation:
If your transaction is going to one database, than it's better to do transaction in stored procedure.The other way can be caused only by logistic issue (DBA don't like you, or he is on vacation). If you call different transacted sources (SQL Server and Oracle) in one transaction - than there is no choice other than do transaction in code.
Upvotes: 1
Reputation: 3444
This isn't a business logic issue, it's a data integrity issue and I feel that is ok to do in the stored procedure. I like to keep transaction logic as close to the operations as possible to shorten their duration.
Upvotes: 4
Reputation: 14703
Or you can do it in both. Check the link: https://web.archive.org/web/20210513004758/https://www.4guysfromrolla.com/webtech/080305-1.shtml
Upvotes: 1
Reputation: 88345
TransactionScope is a really nice way to manage transactions in code. It allows you to nest transacted code across multiple methods, and automatically scales up to distributed mode, if necessary.
I prefer to use TransactionScope over stored proc transactions, because it gives you a lot more control in the code.
Upvotes: 4