SQL Server database transactions (1)-What is a transaction

Source: Internet
Author: User

A transaction is a single unit of work. If a transaction succeeds, all data modifications made in the transaction will be committed and become a permanent component of the database. If the transaction encounters an error and must be canceled or rolled back, all data modifications are cleared.

For example, a bank transfer is a transaction. Transfers can be simply divided into two steps: one is to deduct the money from your account, and the other is to save the money to the account you designated to transfer. These two steps are a transaction that must be executed in full or not in all. If the money is successfully deducted from your account but the transfer fails, the money you transfer will be lost. If the deduction fails but the transfer is successful, the bank will lose.

A transaction is a series of operations performed as a single logical unit of work. A logical unit of work must have four attributes: atomicity, consistency, isolation, and durability. Only in this way can a transaction be made.
Atomicity: A transaction must be an atomic unit of work. modifications to its data must be either executed or not executed.
Consistency: when the transaction is completed, all data must be consistent. In related databases, all rules must be applied to transaction modifications to maintain the integrity of all data. At the end of the transaction, all internal data structures (such as B-tree indexes or two-way linked lists) must be correct.
Isolation: the changes made by the concurrent transactions must be isolated from those made by any other concurrent transactions. When a transaction identifies data, the data is in either the State before another transaction is released, or the State after the second transaction is modified. The transaction does not recognize data in the intermediate state. This is called serializability because it can reload the starting data and replays a series of transactions so that the State at the end of the data is the same as that of the original transaction execution.
Durability: after the transaction is completed, its impact on the system is permanent. This modification will be maintained even if a system failure occurs.

SQL Server runs in the following transaction modes.
Automatic transaction commit: each separate statement is a transaction.
Explicit transaction: each transaction starts explicitly with the begin transaction statement and ends explicitly with the commit or rollback statement.
Implicit Transaction: The new transaction is implicitly started when the previous transaction is completed, but each transaction is still explicitly completed using a commit or rollback statement.
Batch transaction: the transaction can only be applied to multiple active result sets (MARS). The explicit or implicit transaction started in the Mars session becomes a batch transaction. When the batch processing is completed, the batch transaction that is not submitted or rolled back is automatically rolled back by SQL Server.

There are three ways to use transactions in SQL Server: SQL statements, sqltransaction, and transactionscope. The following section describes how to use these three transactions. Http://blog.csdn.net/tjvictor/archive/2009/04/14/4074240.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.