MySQL affairs, the article is enough

Original link: HTTPS: //blog.ouyangsihai.cn/ >> MySQL affairs, the article is enough

Before reading this article, we review the previous several series of articles about MySQL, you should read the following article for help.

What is the transaction 0

Transaction (Transaction) is the basic unit of concurrency control. The so-called transaction, it is a sequence of operations, these operations are either executed or not executed, it is an indivisible unit of work. A transaction is a database maintaining data consistency of the unit, at the end of each transaction, can maintain data consistency.

At the same time, the transaction has a strict defined, must meet four properties, which is what we have been saying ACID, however, does not mean that the various databases will certainly meet four characteristics, for the realization of different databases, in different degree is not necessarily fully meet the requirements of, for example, Oracle database, the default transaction isolation level is READ COMMITTED, that does not meet the requirements of isolation.

Let us build on the progress, to introduce the four characteristics must know will be of affairs, these characteristics also in the interview, the interviewer knowledge of MySQL interview when asked more questions, so be sure these properties need to understand and thorough in mind, a joke, was hit by a train, we should not forget these four characteristics!

Four characteristics of a transaction

Referred to as the four transaction properties of: ACID, namely atomicity, consistency, isolation and durability .

Below us one to explain.

  • Atomicity (Atomicity)

Atomicity refers to the entire database of the transaction is an indivisible unit of work, each should have an atomic operation.

When we execute a transaction time, if a series of operations, one operation fails, then the state needs to perform before all these affairs recovery operations in one transaction, this is the atomicity of transactions.

The following give a simple example.

i++;复制代码

Above this the most simple code will be asked often, this is an atomic operation it? It is certainly not, if we put this code into a transaction, when i+1something goes wrong, the entire code after the rollback is i ++ (i = i + 1 ) , so the rollback, the value of i is not changed.

These are the atomic concept.

  • Consistency (consistency)

Consistency means that the transaction will transform the database from one consistent state as a state, that is to say before and after transaction execution, these two states should be the same, that is, integrity constraints of the database will not be destroyed.

Also, note that the consistency is not concerned about the intermediate state, such as bank transfer process, you transfer to others, as the middle of the state, you are less than 500, he was more than 500, the intermediate state does not care, if division multiple transfer times intermediate state is not visible, only the state final success or failure is visible.

If the consistency of distributed, can be divided into strong consistency, weak consistency and eventual consistency on these concepts, you can look up your own, it is very interesting.

  • Isolation (Isolation)

Services we can open a lot of, MySQL database can be started at the same time a lot of affairs, however, between the transaction and the transaction they are separated from each other, that is, independently of each other, this is a transaction isolation .

  • Persistence (durability)

Transaction persistence means that once the transaction is committed, that is permanent, that is a problem, the database also can be restored. Therefore, durability ensure high reliability issues.

Category 2 transaction

The transaction can be divided into many types, generally divided into: a flat transaction, the transaction with the flat savepoint, the transaction chain, nested transactions, a distributed transaction .

Flat Affairs

Flat affairs is the simplest one, up to a transaction in the actual development is used. In this transaction, all operations are on the same level, the most common way is as follows:

BEGIN WORK
Operation 1
Operation 2
Operation 3
...
Operation N
COMMIT WORK复制代码

for example

begin work;

select * from user;

update user set name = 'sihai' where id = 1;

commit work;复制代码

Flat major transaction disadvantage certain part of the transaction can not be committed or rolled back, or in several separate steps to be submitted.

With flattened transaction savepoint

In addition to this transaction operations support flat transaction support, the biggest difference between this transaction with flat transaction is allowed to roll back to the same transaction earlier in the course of a state of affairs , because the process may perform certain transactions errors that occur will not have all operations are invalid, discard the entire transaction does not meet the requirements, costs too much. Save points used to inform the system should keep in mind the current state of affairs, so that later when an error occurs, the transaction can be returned to the state.

for example

begin work;

select * from user;

savepoint t1;

update user set name = 'sihai' where id = 1;

savepoint t2;

commit work;复制代码

Through the above way we created two save points t1, t2, through ROLLBACK TO SAVEPOINT t1, we can return to 保存点t1.

Chain Services

Chain transaction: a transaction at the time of filing, releasing a data object, the necessary transaction processing context is implicitly transmitted to a start. It should be noted, commit the transaction and operate under a transaction operations will be merged into a single atomic operation is the next transaction can see the results on a transaction.

Chain Affairs, refers to when the rollback, only recently restored to a save point; and flat transaction with a saving point you can roll back to any point of the right to save.

for example

begin work;

select * from user;

savepoint t1;

update user set name = 'sihai' where id = 1;

savepoint t2;

commit work;复制代码

Or example, but for the chain transactions, it is not directly rollback to save point t1, and most can be restored to the most recent save point t2; In addition, we need to pay attention , chain transaction after the commit will release the current office holders All the locks, and flat with a saving point of affairs will not affect the locks held.

Nested Transactions

In another transaction nested transaction, this structure is a bit like a sideways tree structure, located in the root of the transaction called top level transaction. Precursor transaction is called a parent transaction, other transactions known as sub-transactions. Precursor transaction is called a parent transaction, the transaction under the sub-layer called the transaction.

Child transaction can be submitted either rolled back, but it does not take effect immediately commit operation, unless submitted by the parent transaction. Therefore, we can determine, any sub-transactions are actually being submitted only after the top-level transaction commits. Similarly, any transaction rollback will cause all of its sub-transaction rollback together.

BEGIN WORK
     SubTransaction1:
             BEGIN WORK
                 SubOperationX
             COMMIT WORK
     SubTransaction2:
             BEGIN WORK
                 SubOperationY
             COMMIT WORK
     ...
     SubTransactionN:
             BEGIN WORK
                 SubOperationN
             COMMIT WORK
COMMIT WORK复制代码

Distributed Transaction

Generally it refers to a distributed transaction in a distributed transaction flat running environment, requiring different access nodes location data network according to.

In different physical addresses, through network access, execute different transaction, which is distributed transaction.

Use 3 transactions

First, this part of the statement we first introduce these matters, not a lot, is not complicated to use, with a table below to make a sort.

Note : COMMITand COMMIT WORKstatements except that the COMMIT WORK is used to control the behavior after the end of the transaction is CHAINor RELEASE, if it is CHAIN, then the transaction is a chain of transactions .

User parameters can be completion_typecontrolled as follows:

  • Example completion_type = 1

Perform the following operations;

SET @@completion_type = 1;

BEGIN WORK;

INSERT INTO lock_test SELECT 10;

COMMIT WORK;复制代码

Then we'll do the following;

INSERT INTO lock_test SELECT 115;

ROLLBACK;

 SELECT * FROM lock_test;复制代码

Let's insert a data 115, and then rolled back, we know that if not in a transaction, when 115 should be inserted success, even if we roll back, but here we rolled back after the query results are as follows:

This time was not 115 this record, which is the rollback take effect, explained COMMIT WORKlater, is a new transaction, so will this outcome.

  • Example completion_type = 2

We first perform the following operations;

SET @@completion_type = 2;

BEGIN WORK;

INSERT INTO lock_test SELECT 5;

COMMIT WORK;复制代码

We have already submitted the transaction, and when we use the following statement to query data lock_test of time, there will be disconnected ** **.

SELECT * FROM lock_test;复制代码

Transaction isolation level 4

There are four transaction isolation levels are:

  • READ UNCOMMITTED
  • READ COMMITTED
  • REPEATABLE READ
  • SERIALIZABLE

With respect to these isolation level will bring the issue and concluded that, you can view this article: MySQL is another artifact - lock, MySQL essential interview

5 summary

This article describes the contents of a bit from the following several content MySQL database transactions, and other content in more detail and then explain in a later article.

  • concept
  • Transaction Type
  • Use affairs
  • Transaction isolation level

Articles have inappropriate, please correct me, if you like micro-letters to read, you can also concerned about my micro-channel public number : , 好好学javaaccess to quality learning resources.

This article from the blog article multiple platforms OpenWrite release!

Guess you like

Origin juejin.im/post/5db90231518825645a5ba74d