MySQLServer → 5 transaction processing _ MySQL

Source: Internet
Author: User
MySQLServer → 5 transaction processing bitsCN.com

My SQL Server → 5 transaction processing

. Concept of transactions

The transaction mechanism treats a group of database operation commands as a complete and independent operation sequence, that is, a group of commands can either be executed successfully or all fail to be executed.

1. Role of a transaction: either all operations are completed, or all operations are not executed.

2. what is a transaction?

A transaction in a database is a mechanism in which each transaction is an independent unit of work. it contains a group of database operation commands, and these commands are either executed or not executed, therefore, a transaction is an inseparable unit of work logic.

3. transaction attributes:

① Atomicity: each element of a transaction is inseparable.

② Consistency: when the transaction is completed, all data must be consistent.

③ Isolation: all concurrent transactions that modify data are isolated from each other.

④ Persistence: as long as the transaction is successfully committed, it cannot be rolled back to the status before submission.

· Use of transactions

1. create a transaction:

① Start TRANSACTION: BEGIN TRANSACTION

② Commit transaction: COMMIT TRANSACTION

③ Rollback transaction: ROLLBACK TRANSACTION

Category:

Show transactions: Use begin transaction to specify the start of the transaction, and subsequent T-SQL statements are a whole.

Implicit transaction: use the Set implicit_transaction on statement to enable the implicit transaction.

Automatic transaction commit: it is the default mode of SQL Server d. It treats each separate T-SQL statement as a transaction. if it is successfully executed, it is automatically committed. if it fails, automatic rollback.

Commonly used is to display transactions, which clearly specify the boundary at which the transaction starts. The fewer statements a transaction contains, the better. do not place irrelevant statements in a transaction, such as variable declaration and value assignment.

2. principles for creating transactions:

① The transaction should be as short as possible.

② Minimize the amount of data accessed in transactions.

③ Do not open transactions when browsing data.

④ Do not request user input during transaction processing.

. Use transactions in the stored procedure

1. considerations for using transactions:

① After each operation, the value of @ error or @ rowcount is checked.

② After a transaction ends, the T-SQL code that follows the transaction can continue to be executed, but the transaction cannot be rolled back after an error occurs.

③ The committed transactions will formally write the data into the database.

④ When a transaction is being executed, in the event of an accident such as a power outage, the transaction will be automatically rolled back when the system restarts again.

⑤ If an error occurs in the transaction instance, the transaction cannot be executed and the transaction will be rolled back automatically.

⑥ Statements that cannot be rolled back cannot be used in transactions.

Example:

Create database

Alter database modify database

Drop database delete database

. Transaction isolation level

① Read uncommitted: data is not isolated. other transactions can modify or delete the data at the same time even if the transaction is in use.

② Read committed: data that has not been submitted cannot be Read.

③ Repeatable Read: the data Read by the subtransaction is locked and cannot be modified or deleted by another user.

④ Snapshot: Snapshot isolation, which can provide a committed version of the required data for transactions that read data. Therefore, transactions that write data are blocked from transactions that do not read data.

⑤ Serializable: locks all data tables used by the transaction, and does not allow other transactions to add, modify, or delete data.

BitsCN.com

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.