MySQL learn column is continuously updated in :)
Article Directory
Transaction Transaction Transaction Control Language TCL
Transaction control language TCL Transaction Control Language
A set of sql statement as an indivisible unit of execution that matters
must be fully implemented or total failure
Example: Taobao to buy things
that you have to pay two-step business sql statement
one is your money deducted
a business money is added
Assuming that the first sentence of the second sentence of execution does not perform so cold and vice versa
Executing the transaction as long as there is a part of the statement will undo all failed because the data is not the final data we thought of
it will roll back all statements are no state had executed
Detailed look back here roughly an impression
ACID
Includes transaction ACID properties
A - atomicity indivisible atomic
C - consistency change from one consistent state to another consistent state of the individual that is complementary indivisible
because abnormal system or database system failure, resulting in only partial data update success, but this is not the final data we need, this is the inconsistent data
in short Yongfu enjoy with hard work hand in hand with when no one is left behind
I - isolation isolation execute a transaction not interfere with other transactions
D - Durability persistence also after the change is permanent database (commit) or other operations following a database failure
ought not to have been submitted on a transaction have any impact
autocommit
autocommit property defaults will sentence sql statement as a transaction
of course, we often need more sentences into one transaction
would need to close this multi-function and multi-select sql sentence of the first performance
SHOW VARIBLE LIKE 'autocommit';
SET autocommit=0;
start transaction;#可以省略
语句1;
语句2;
# 事务语句 增删改查 select insert update delete
commit; #github里面的 commit 还记得不?不是指“评论”,而是指提交更改
Before commit data only in memory not apply the changes to (apply commits) disk
is assumed that commit after the preceding statement did not execute execution error occurred that this transaction is over the perfect ending
commit and rollback;
Transaction Execution Language
American National Standards Institute (ANSI) has developed management standard SQL database transactions.
, Sql statement behind the order of execution from the beginning of the implementation of the transaction (start transaction), we are content to be, until the following one of four events:
1️⃣ encountered (the REACH) COMMIT statement , in modify memory have been executed, both permanent record of all, before the transaction (or formally apply to officially apply to) in the database. At this point commit statement also marks the end of the transaction
2️⃣ encountered (the REACH) ROLLBACK statement , all prior to the transaction have been executed in the modified memory are canceled, the database is rolled back to a previous state (consistency);
3️⃣ all sql statement the successful implementation of the program to the end there are no errors , then the effect is equivalent to encounter COMMIT;
4️⃣sql statement is executed, the error occurs , then it is equivalent to experiencing a rOLLBACK, the database rollback to a previous consistent state.
Storage Engine
Data storage (used in) technology used in the document process
we mentioned: In the modify memory executed, have a permanent record of the transaction (or formally apply to officially apply to) in the database
record of the process completed by the storage engine but unfortunately
MySQL three commonly used storage engine innodb myisam memory
only innodb support services
View your own database engine code used:
SHOW engine;
Transaction concurrency problem
Isolation (isolation) mentioned earlier is actually ideal for
a transaction or another transaction will be more or less affected by interference
then the transaction concurrency (simply understood as the simultaneous execution)
are three common concurrency problems:
dirty reads,
non-repeatable reading
phantom read
our next section, and transaction isolation levels together to explain
Transaction isolation level
Concurrency issues arise we have to strengthen control, it would have set the policy setting transaction isolation level issue
There are four isolation level from low (control, the degree of interference suppression looser) to high (control, more stringent disturbance suppression)
were
the Read Uncommitted
the Read committed
Repeatable Read
the Serializable
oracle only supports two isolation levels (the Read Uncommit & Serializable)
mySQL supports four
The following examples elaborated on by isolation level, and described in detail,
why set the isolation level to solve the above-mentioned transaction concurrency problems Dirty read Non-repeatable read Magic Reading
set the isolation level of how to solve the problem
1️⃣Read uncommitted
Uncommitted Read, by definition, a transaction can read another data uncommitted transactions .
Case :
Nguyen chicken dish wage is 10,000 / month.
When wages, accounting accidentally pressed the wrong number, according to 12000, the money has hit the programmer's account, but we know that banks have a mechanism to go back on a day that is a transaction database has not been submitted ,
at this time, Nguyen dish of chicken to see their wages, found more than 2,000, that a raise, is preparing to spend (cut hand!) yet.
But accounting reconciliations, accounts not found, immediately withdraw the transfer (the database is: rollback ),
Nguyen chicken dish to shopping mall checkout dumbfounded when
ye more than two thousand gone QAQ ...
Analysis : Nguyen dish of chicken to see when accounting data is not to commit the transaction. This is the dirty read .
How to solve that dirty read it? Read committed!
2️⃣Read committed
Read committed, by definition, it is a transaction to wait for another transaction commits after to read the data.
Case :
Nguyen chicken dish wages to 1w, he took the credit card to enjoy life,
when he pays the bill (chicken dish Affairs Nguyen open), charging systems detected in advance of his Cary 10,000,
at this time! ! Nguyen's father chicken dish all the money transferred out as a stock investment,
when not submitted, of course, check your balance or your friends 1w This avoids the dirty read
but came out bug:
Nguyen chicken dish Dad submit operation deducting money to buy shares confirmed .
When Nguyen checkout chicken dish, that is, the toll system is ready to charge, and then detecting the amount of Cary,
found: no money
Nguyen dish is chicken Affairs at the time of the operation table, not aware of other matters have been perpetrating a fraud, the changed data
that is non-repeatable read it Raney card balances are dishes of chicken read unrepeatable (instability)
That may be how to solve the non-repeatable read problem? Repeatable read!
3️⃣Repeatable read
Repeated reading, that is, start reading data (open transaction), no longer allowed to modify the operation of
the previous study we know for contact update Update operation, the transaction is running blocking the transaction synchronization call it a transaction execution for a table, another transaction can not mess things past
At this time Nguyen dish of chicken consumption (chicken dish Affairs Nguyen open), no longer have to worry about someone else the money spent
Analysis : Repeatable Read can solve non-repeatable read problem.
Written here, it is to be understood that, corresponding to non-repeatable read is modified, i.e. UPDATE operation.
But it may also have phantom read problem. Because the problem phantom read operation corresponding to the insertion INSERT, UPDATE operation instead.
What will appear when the phantom read?
Note that we say to update Update operation, the transaction run blocking , the transaction synchronization call that is, serial execution , but the insertion operation or the non-blocking execution, or the asynchronous call is executed in parallel
Example 1:
A transaction T1 is viewing or modifying data when (such as a table), another transaction T2 quiet Mimi added to the table a little something, cause, T1 is performed again found the time to see more than a loud noise like this is an illusion general magic reading
Example 2:
If you want to perform T1 is to modify the operation, might have serious consequences,
for example, the teacher (teacher affairs) would like to give an all-boys class, everyone changed Gender Male (all rows modify object instances data), the results have not submitted,
teaching at the (Education Department) said that in your class to the girls, iNSERT data inserted a girl did not tell the teacher
the teacher such a change submitted
and a sister was a changed man :)
That phantom read how to solve the problem? Serializable!
4️⃣Serializable serialization (i.e. serialization)
Serializable transaction isolation level is the highest, at this level, the transaction serialization order of execution, all operations are blocked , the synchronous call is
possible to avoid dirty reads, non-repeatable reads and phantom reads .
This transaction isolation level - but inefficient (serial you know), more consumption of database performance, generally do not use.
5️⃣ most database default transaction isolation level is Read committed, such as Sql Server, Oracle.
Mysql default isolation level is Repeatable read.
实践 read uncommitted
We feel it to be a small case when the isolation level is set incorrectly why dirty reads can cause phantom reads Non-repeatable read
1️⃣ We use MySQL console black-rimmed sub cmd Login SQLyog because it is a single user
See Section II do not know the method of transmission: MySQL database learning (two) - Configuration Log operating method MySQL installation environment
2️⃣ We use two analog cmd framed two user access to the database user operation is a transaction
that is two transactions on the same table operation
The basic idea is that employees table Nguyen chicken dish department number changes from 90 to 60 of
this change is responsible for the affairs of 1 T1
and
want to check my affairs department number between 2 T2
happen so what will be an interesting thing? We'll see:
3️⃣ we first have to put the transaction isolation level to change a dirty reads this will cause phantom reads Non-repeatable read level
here to teach two sentences
a query the current transaction isolation level:
select @@tx_isolation;
Visible is not the lowest level,
Two is we set a minimum level
set session transaction isolation level read uncommitted
Then check:
ok cmd two boxes are so dry again remember
is the two transactions (T1 left the right side of T2) isolation level were reduced:
Because the use of the Chinese remember the two transactions are executed:
set names gbk;
Before updating my department number
is what I query:
use data1;
select concat_ws('-',last_name,first_name) as 名字,department_id as 部门号 from employees where last_name='阮';
T1 then execute entry (Update Update) my new department number, sentence by sentence-Ha
use data1;
set autocommit=0; #这里开始 就默认有一句start transaction; 了 所以这里开始就可以输语句了
update employees set department_id=60 where last_name='阮';
This is the update
already visible "change" a
real change yet? ? ?
I said this is only the value of memory has not been applied commit it!
Then Affairs 2 T2 quiet Mimi Nguyen would like to check the department number of chicken dishes:
eh? Nguyen chicken dish is already No. 60 departments, ah?
This is the dirty read phenomenon
at this time is to see the non-repeatable read (not a phantom read the magic of reading for T1 when adding a new row phenomenon when I was this change )
Did not commit at this time T1 and T2 Dang Houshua I can put it this:
At this time T2 and then check the wave of senseless force:
At this time T2 has not believe my eyes so confused Nguyen dish of chicken in the end is which department
this when we
finally changed to 60 and the final nail in the future commit your server hardware explosion theory this operation nor rollback, you can try:
Practice read committed
Or the above example Nguyen chicken dish sector number 60
we isolation level T1 T2 are changed to read committed
and then T1 changes id = 90 results were as follows:
At this time T2 want to check the
note plus a commit; query after query also belong Affairs category! ! !
Impact query behind the front will
Then we can see that while the T1 changed (60-> 90) T2 60 is displayed
indicating that no commit updates T1 and T2 is not non-repeatable read or dirty read
Other isolation level of practice
Other smart isolation level you can test yourself
test Serializable the serialization when the attention of
Magic Reading T1 is modified before (content may be the name of all rows into abc) T2 insert a new line T1 cause the object to modify more more (that is, more than a line)
in fact, the intention is to modify T1 T2 before time did not add all the rows
which resulted in phantom read this case only if the isolation level set to serializable the serialization of time to avoid
the other is cold
to sum up
This section a bit more but I think this transaction loaded to force the concept should be straightened