Day6 Spring transaction management (1)

Before understanding the business, talk about your daily life, most commonly a dry thing: money.

For example, you go to the ATM machine to take 1000 dollars, roughly two steps: First, enter a password amount, bank cards and 1,000 yuan deducted; then the ATM 1000 dollars. These two steps must be executed either perform or not. If the bank card was charged for the 1000 ATM money but if it fails, you will lose 1000 yuan; if the bank failed but ATM card deducting money but out of 1000, the bank will lose $ 1,000. So, if a step fails another successful step for both sides is not a good thing, no matter what if a step fails later, the whole process can be rolled back to collect the money, which is totally cancel all operations, then, that both sides are excellent .
Transaction is used to solve similar problems. A transaction is a series of actions, which is integrated with a complete unit of work, these actions must be completed, if there is a failure, then the transaction is rolled back to the beginning, as if nothing had happened .

What is a database transaction

Four characteristics of database transactions (ACID):

  • Atomicity (Atomicity): the transaction is an atomic operation, a series of actions. Atomicity of transactions to ensure that actions are either completed or totally ineffective.
  • Consistency (Consistency): Once the transaction is completed (regardless of success or failure), the system must ensure that its business model in a consistent state, but will not be part of the complete partial failure. In reality, the data should not be destroyed.
  • Isolation (Isolation): There may be many transactions will also deal with the same data, and therefore should be open to every transaction with other transaction isolation to prevent data corruption.
  • Persistent (Durability): Once the transaction is completed, no matter what system error occurs, its results should not be affected, so you can recover from any system crashes. Under normal circumstances, the results of the transaction is written to persistent memory.

A: Atomicity Atomicity

What is?

Transactions executed either succeed or fail, will not only perform part.

How did you do it?

  • When a transaction begins, set begin;
  • When finished, execute commit;
  • On failure, the implementation of rollback;
    Note: commit and rollback will only execute one.

rollback (rollback) is how to do?

The undo_log rollback is implemented, prior to each database modification operation, will be written to the value before the modification undo_log recording. Rollback according undo_log perform a reverse, to restore data to the state before the transaction;

C: Consistency Consistency

Consistency is:

  • The most important principle in ACID, is the ultimate goal;
  • Consistency state;
    • When the transfer, the amount of individual parties may have changed, but both the total amount unchanged;
    • Analogy: the law of conservation of energy, the energy transfer can not be increased or disappeared out of thin air;

I: Isolation Isolation

Deal with what scene?

Concurrent transactions that have multiple transactions simultaneously in progress. Multiple transactions simultaneously operate in the same database, it may have an impact on each other.

When transactions to execute concurrently, in the end there will be what impact?

  • Dirty read, read other uncommitted transaction value, and these values ​​may be uncommitted invalid value (for example, rolled back)
  • Non-repeatable read, i.e., read data twice changed;
  • Phantom read, i.e., when data is read twice, before the data is generated does not exist;

Isolation is how to manage the impact of the above?

By setting different transaction isolation levels. Isolation level See section below.

D: Durability Persistence

After the transaction commits, the data will be stored on persistent device. Popular speaking, is going to write the hard disk.

Random IO write efficiency is poor, in order to improve efficiency, the WAL (Write Ahead Log) technology, with reference to the HBase Hlog, MySQL corresponds to redo-log.

Isolation Levels

Find data for the current isolation level:show variables like "%isolation%";

MySQL default isolation level is: REPEATABLE-READ

Oracle, SQL Server, their default isolation level is REPEATABLE-READ it?

read-uncommitted

read-uncommitted: uncommitted read, can not read the data submitted, it will result in "dirty read."

Setting the isolation level:mysql> set transaction_isolation="read-uncommitted";

Dirty read Examples:

time A transaction Transaction B
T1 begin;
T2 begin;
T3 select money from account where id = 1; // money initial query is: 0
T4 select money from account where id = 1; // money initial query is: 0
T5 // update the money 100 update account set money = 100 where id = 1;
T6 Money Account WHERE ID from SELECT =. 1;
// can now read the uncommitted values: 100
// read uncommitted values, called "dirty reads"

read-committed

read-committed: read committed, uncommitted data is not read, will read the submitted data.

If not an invalid value is read uncommitted, but some applications may require that within the current transaction, data can be read repeatedly (ie, multiple reads the value is the same). Read Committed Repeatable Read is not resolved.

Setting the isolation level:mysql> set transaction_isolation="read-committed";

Non-repeatable read Example:

time A transaction Transaction B
T1 begin;
T2 begin;
T3 select money from account where id = 1; // money initial query is: 0
T4 select money from account where id = 1; // money initial query is: 0
T5 // update the money 100 update account set money = 100 where id = 1;
T6 Money Account WHERE ID from SELECT =. 1;
// the value read this time is not changed, is still 0
T7 commit
T8 Money Account WHERE ID from SELECT =. 1;
// this case the value read vary for other transaction commits, it becomes 100
// within the same transaction, read many different data, a "non-repeatable read."

repeatable-read

Due to historical reasons, repeatable-read for MySQL default isolation level. Note, however, that the isolation level of efficiency is not high, such as Oracle, SQL Server, etc. are using as the default read-committed isolation level.

Within the current transaction, all data read will no longer be updated, repeated reading, the data will not change.

Note, MySQL's repeatable-read solves the phantom read problem.

Serializable

Serialization: All operations are serialized, reading will be locked. Efficiency is very low, basically not used.

Read and write data

Write lock

Within a transaction, the bank will write-write lock, then other transactions, the bank can not make changes unless the current transaction commit / rollback, the lock is released.

Write lock can be resolved:

  • Write critical resource conflicts;
  • Modify lost;

read

Read and locked it?
Read in the end what happened?

Snapshot Reading

Snapshot read:

  • Data has many versions;
  • MVCC: multi-version concurrency control protocol;
  • Read a version in which, the version with the isolation level related;
  • What is a snapshot reading? For example: select * from account;
  • Benefits: fast, supports high concurrency.

MVCC (multi-version concurrency control protocol)

  1. The repeatable-read isolation levels

    • (Not deleted) version will start reading data at the time the transaction has been submitted;
    • Throughout the transaction, only read the version of the data, the update will not affect any changes;
  2. The read-committed isolation level

    • Time data is read, the data submitted (not deleted);
  3. read-uncommitted isolation level under:

    • No concept version, all data is updated in the database, only the most recent data can be read.
  4. serializable

    • No concept version, will read lock.

Current Reading

Current reading: only reads the latest value of the current.
Example:
update account set money = money - 50 where id = 1;
and all modification commands,
plus:

  • select * from account where ... lock in share mode: Read data, and shared locks.
  • select * from account where ... for update: Read data, and adding the exclusive lock / mutex.

Guess you like

Origin www.cnblogs.com/cheng18/p/12104597.html