What is business
Business is a series of DML( Additions and deletions ) The execution of the statement is successful , Or all failed . Once executed successfully, it will exist forever .
Why business is needed
for example ： The amount of the card is 1000 element , Li Si's bank card amount is 1500 element . At this time, if Li Si transfers money to Zhang San 200 element , Suppose we divide the transfer of money here into 2 strip sql perform , They are Li Si's amount -200 The amount of yuan and Zhang San +200 element . First carry out Li Si's amount -200 Yuan this sql And it worked , Then execute Zhang San's amount +200 Yuan this sql But it failed , At this time, Li Si's amount became 1300, But Zhang San's amount has not changed . We don't allow this to happen , This is when we need to use transactions .
Four characteristics of transactions
Atomicity ： Either all statements are executed and submitted successfully , Either all statements fail to execute and roll back . Partial success is not allowed 、 Part of the failure .
Uniformity ： When the transaction completes , The state of the database must be consistent , Is the database from a normal state to another normal state .
Isolation, ： When multiple users operate on the same data at the same time , There will be multiple transactions , Every transaction is independent , One transaction does not affect another .
persistence ： Once the transaction is committed successfully , Then the data will persist , Even if the system sends a fault .
The isolation level of the transaction
Read uncommitted (Read uncommitted)： When a transaction begins to write to a row of data , Other transactions cannot write to the row data at the same time , But it can read . Transactions cannot operate the same data at the same time to avoid update confusion , But it's time to read data , Once a transaction is rolled back, dirty reads are created .
for example ：account Tabular balance The original value is 500,A Business balance Modified into 300 But the transaction is not committed ,B The transaction comes to read this at this time balance Value reads 300,B After the transaction is read A Transactions need to be rolled back for some unusual reasons ,balance It's back to 500, This is the time B Transaction read 300 Namely Dirty reading
Read submitted (Read committed)： When a transaction reads data , Another transaction can write , However, when the write operation is in progress, other transactions can no longer read and write . This avoids dirty reading , But it will cause It can't be read repeatedly
for example ：A Transaction read first account Of balance The value of is 500, At this time, other transactions can be written ,B The business is A After reading the transaction for the first time balance Changed to 300,B After the transaction write operation is completed A Read the business again balance It will read balance=300, With the first reading of balance=500 atypism , And that's what happened It can't be read repeatedly
Repeatable (Repeatable read)： When a transaction is not finished reading and writing data , Other transactions are not allowed to read or write the data . This avoids dirty reading and non repeatable reading, but at the same time it causes Fantasy reading
for example ：A Business needs to put account The whole of the watch balance Modified into 0, When A When the transaction is executed B Business to account Table inserts a balance=300 The data of , After two transactions are completed, it may be found that not all of them balance All changed to 0, There will be one balance=300 The data of , This is it. Fantasy reading
Serializable (Serializable)： Each transaction must be queued, executed one by one , Two or more transactions are not allowed at the same time . It completely avoids any problems caused by transaction parallelism .
Safety factor ： Serializable > Repeatable degrees > Read submitted > Read uncommitted
Coefficient of performance ： Read uncommitted > Read submitted > Repeatable > Serial number
It can be seen that the relationship between safety and performance of isolation level is inversely proportional , because The principle of isolation level is through locking or serialization
MySql Have the above four isolation levels , The default isolation level is repeatability (Repeatable read). and Orcale There are only two isolation levels, read committed and serializable , The default isolation level is read committed (Read committed)
Redo log And Undo log
Because of the concept of transactions , So when we operate the data, we do not simply operate the data in the disk directly , It will also involve Undo log Files and Redo log file , For example, we make a update When operating, it will record Undo log as well as Redo log, When we need to roll back data , We need to use the undo log Roll back to ensure atomicity , When we need to persist data, we need to use Redo log To ensure durability
Redo log principle ： To ensure durability , Every time you're done sql Records to Redo log, Downtime occurs while data is still in memory , Can pass Redo log To do persistence ,Redo log It's execution sql It is then recorded and persisted to disk before the transaction is committed .
Undo log principle ： To ensure atomicity , Every time you execute one sql There was always a record of the opposite sql( For example, execute a clause inserts sentence ,Undo log I'll record one delete sentence ), When the data needs to be rolled back, it can be done through Undo log To complete ,Undo log It's execution sql And q Previous record .
Let's use the example of Zhang San Li Si's transfer at the beginning to sort out the process , Zhang San's balance=1000, Li Si's balance=1500, Li Si transfers money to Zhang San 200
Three characteristics of transactions in the learning process ( Atomicity , Isolation, , persistence ) There are traces to follow , Only consistency has no relevant principle information , And there are many ambiguities . As I understand it , Probably the principle of consistency is atomicity 、 Isolation and persistence , Their ultimate goal is to keep the database in a normal state