Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2005: Why Name Transactions?

I've been sorting out the whole nested transaction thing in SQL server, and I've gleamed these nuggets of understanding of behavior of nested trans':

  • When nesting transactions, only the outermost commit will actually commit.
  • "Commit Trans txn_name", when nested , will always apply to the innermost transaction, even if txn_name refers to an outer transaction.
  • "ROLLBACK TRAN" (no name) , even in an inner transaction, will rollback all transactions.
  • "ROLLBACK TRAN txn_name" - txn_name must refer to the outermost txn name. If not, it will fail.

Given these , is there any benefit of naming transactions? You cannot use it to target a specific tranasction, either for commit or rollback. Is it only for code commenting purposes?

Thanks,

Yoni

like image 569
user144133 Avatar asked Aug 13 '09 17:08

user144133


People also ask

Why do we need SQL transactions?

For any business, transactions that may be comprised of many individual operations and even other transactions, play a key role. Transactions are essential for maintaining data integrity, both for multiple related operations and when multiple users that update the database concurrently.

What are SQL Server transactions?

A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.

What does the term transaction mean in SQL?

A transaction is a logical unit of work that contains one or more SQL statements. A transaction is an atomic unit. The effects of all the SQL statements in a transaction can be either all committed (applied to the database) or all rolled back (undone from the database).


2 Answers

Effectively it's just a programmers aide memoire. If you're dealing with a Tx that has a number of inner transactions, giving each meaningful names can help you make sure that the tranactions are appropriately nested and may catch logic errors.

like image 96
Chris J Avatar answered Oct 22 '22 09:10

Chris J


You can have procedures rollback only their own work on error, allowing the caller to decide wether to abandon the entire transaction or recover and try an alternate path. See Exception handling and nested transactions for a procedure template that allows this atomic behavior.

like image 34
Remus Rusanu Avatar answered Oct 22 '22 10:10

Remus Rusanu