MySQL to explore (iv)----MySQL transaction

Source: Internet
Author: User

  1. What is a transaction: A popular explanation is a complete set of operations on a database that contains one or more operations in a complete set of operations. It's too low to explain it. Official: A transaction is a complete logical unit executed in the DBMS that contains one or more operations.
  2. The role of the transaction: it says the definition of the transaction, but it does not reflect the importance of the transaction, in fact, the business is a very important concept, we often hear some related nouns: rollback, isolation (of course, this is only to say that the database, in fact, the meaning of the two concepts more extensive), this is the role of affairs.
        • Rollback: The transaction acts as a logical unit with rollback. Rollback means that we are able to roll back the transaction and revert to the state before the transaction begins when we are working on the transaction. Its essence is realized by recording the state of the transaction before it begins.
        • Isolation: Isolation, as the name implies, is to isolate these logical units from interfering with other logical units. In practical applications we will have multiple threads working on the database at the same time, such as a in the modification, B is also modified, if not controlled, it is likely to cause dirty data.
  3. Transaction characteristics ACID:
      • A:atomicity Atomicity, how to understand-that is, there is only one state of the nature, either the execution succeeds, or the execution fails, it can only be in both cases, and only one situation can occur!
      • C:consistency consistency, the consistency is that the data in the database satisfies the integrity constraints.
      • I:isolation isolation means that there are two simultaneous transactions that cannot interfere with one another.
      • D:durability persistence is that the changes made by the firm must be persisted to the hard drive and not easily lost.
  4. Above has introduced the basic nature of the transaction, the role: pull a bunch of theoretical knowledge, now we have to point to the actual combat!!!

We have already introduced the concept of transactions, so how do we operate transactions? There are many ways to do this, like JDBC in Java, or the shell! Below we learn through the terminal, first we should know that a concept called automatic submission, then what is automatic submission? This is very well understood, that is, we do not have to display the transaction commit when the database is being manipulated, and MySQL automatically completes it for us.

A. Check how MySQL is submitted: show variables like ' autocommit '; be able to see if the database is automatically committed.

B. Set the automatic submission mode for MySQL: set autocommit=false; you can set MySQL not to submit automatically.

C. Auto-Submit PK Manual submission: Let's try the difference between autocommit and manual submission:

        • Auto-commit: We have no right to repent after we execute a command! For example, the bank's business, A to B transfer 100 yuan, the bank needs to do two steps, first a bank card minus 100 Yuan, B bank card increase of 100 yuan, but if set to automatic submission, when the first step to complete the bank server down, then the end of the data problems. So the enterprise usually sets Autocommit to false.
        • Manual submission:
          • Open transaction: Mode Begin,start transaction,set autocommit
          • Commit TRANSACTION: Commit
          • ROLLBACK TRANSACTION: Rollback
          • Transaction instance: Give an example to illustrate a transaction: we have such an employee table, Baidu (Name,age,salary);

Here we operate on this table, first set the open transaction through the above method, insert a piece of data, view the database, commit or rollback (note, here we operate at the same terminal, because different terminal design to transaction isolation.) Our next section will learn) as follows:

Above we have inserted a record, we can be viewed at this terminal, we can find in this terminal has been inserted successfully, but if the rollback operation at this time, and then see the data inserted disappears. If committed, the data is persisted to the database.

D. The principle of the transaction: the above operation we will find that we can rollback the transaction when it is not committed, then we need to know why we can rollback it? In fact, MySQL is the way to roll back through the log, we will log the operation within the transaction, if a rollback occurs, MySQL will read the log information to redo (will have been successful but not persisted to the hard disk data to persist), in such a way to ensure consistency. Then undo the part of the transaction that is not committed, using this method to ensure atomicity.

                       

MySQL to explore (iv)----MySQL transaction

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.