[MySQL] database transactions in-depth analysis

I. Introduction

Only InnoDB engine support services, below are the contents of the InnoDB engine as the default condition

Second, the common concurrency problems

1, dirty reads

One transaction reads data from another uncommitted transactions

2, non-repeatable read

A transaction is inconsistent for the same before and after the reading result data. Among other matters to be read twice revised

3, phantom read

Magic Reading refers to a transaction to read a range of data, because the operation of other transactions lead to inconsistent results before and after the two read. Magic Reading and the difference is that non-repeatable read, non-repeatable read for a line is defined in terms of data, and phantom reads are multiple rows of data for uncertainty. Thus usually appears in phantom read range queries with a query in

Third, the transaction isolation level

1, Uncommitted Read (READ UNCOMMITTED)

Possible dirty reads, non-repeatable read, phantom read

2. Read Committed (READ COMMITTED)

Avoid dirty reads, non-repeatable read may occur, phantom read

3, may be repeated to read (REPEATABLE READ) (mysql default isolation level)

Avoid dirty reads, non-repeatable reads. By the interval lock technique avoids the phantom read

4, serialization (SERIALIZABLE)

Serializes concurrent avoid all possible anomalies, but it will greatly reduce the ability of concurrent processing system

Fourth, the database log what?

1, undo log

undo log for storing modified data values ​​before being modified

UNDO LOG is divided into two types, one is INSERT_UNDO (INSERT operation), the only key record insert;

One is UPDATE_UNDO (UPDATE, and DELETE operations comprising a), the only key record modified record and old column.

2, redo log

mysql a transaction will first of all sq l redo log recorded in the redo log record and then synchronized to the data file

It can bring these benefits:

  • When the buffer pool of dirty page has not been flushed to disk when the occurrence crash, after starting the service can be found by redo log records need to be flushed to disk file;
  • Data buffer pool directly flush to the disk file, the IO is a random, poor efficiency, and recording data in the buffer pool to redo log, a sequence is the IO, can improve the speed of the transaction commits;

3, binlog log

For copying from the recording master database is in binary format. It is written to disk after a transaction is committed.

Here note the difference with the binary log of the redo log, redo log storage engine layer is generated, and the binary log database layer is produced. Suppose a large transaction, tba take notes on 100,000 lines inserted, in the process, has continued to record the order of redo log, and the binary log does not record until the transaction commits, will once written to the binary log file in

Fifth, the database transaction control

1, by default, turn on automatic commit the transaction. Each implementation of a sql, corresponding to a transaction will be submitted

2, is automatically set to submit false spring characteristic will be connected to the bottom. Use manual submission

Six, ACID properties of the transaction

1, atomicity (Atomicity)

All operations in the transaction as a whole as indivisible as atomic, either all succeed, or all fail.

2. Consistency (Consistency)

The results of the transaction must be made to the database from one consistent state to another consistent state. Refers coherency states: state of the system to meet a data integrity constraints (primary key, referential integrity, Check constraints, etc.) reflect the true state of the system 2. The database of the present state of the real world to be described, both before and after such transfer the total amount of accounts should remain unchanged.

3, isolation (Isolation)

Concurrent execution of transactions do not affect each other, and their impact on the database when they perform the same serial. For example, multiple users to one account transfers, account should be the result of the final result and transfer them according to the order of the same.

4, persistent (Durability)

Once you have submitted the transaction, it updates to the database is persistent. Any transaction or system failure will not cause data loss.

5, redo log and undo log to achieve the atomicity, consistency, persistence

6, the lock mechanism to achieve the isolation

 

Guess you like

Origin www.cnblogs.com/wangzhongqiu/p/11370606.html