## Quick clarify the database transaction

1. Define the transaction

A transaction is a logical unit of a plurality of simultaneously operating a minimum or a plurality of database tables

2. Business scene

With table operations: the transfer of payments, A transfers to accounts 100 B, then A -100 B account transfers account +100 considered successful, these two operations to be successful, then we need to ensure that their affairs while at the same time the success or failure .

Multi-table operation: In order scenario in the electricity business project in charge, reduce inventory, generate orders these three operations typically involve multiple databases, the same need to ensure the success of these three operations are considered orders success, otherwise it should be rolled back.

3. Engine

Prior to version 5.5 MySQL uses MyISAM, after 5.5 is used InnoDB. The biggest difference is MyISAM table locks / row locks, the transaction.

Four characteristics 4. affairs

ACID:A-atomic、C-consistent、I-isolation、D-durable

Atomicity, a transaction is the smallest unit of logic, only the successes and failures of two states

Consistency, before and after implementation of transaction data should be lawful, subject to the constraints of

Isolation, and the transaction is a transaction between independent, independently of each other's

Data persistence, the branch will fall to disk

5. Isolation Level

When it comes to affairs of the isolation, then you have when it comes to transaction isolation levels, read-uncommited, read-commited, repeatable-read, serializable. These types of isolation level is to solve the dirty read, can not be re-read, phantom read problem.

Dirty read: read data in a transaction between the two, but there is another transaction modifies data not submitted, resulting in inconsistent data before a transaction twice read.
Not reread: In a transaction between two reads data, there is another transaction to modify the data and submitted, resulting in inconsistent data before a transaction twice read.
Magic Reading: reading data in a transaction between the two, there are new rows of data n another transaction occurs when a data is read first read the data does not exist in the second before the transaction leads.

6. isolation scheme

Speaking of the transaction isolation level, then you have to realize when it comes to these types of isolation-level program. There are two main

Multiple Concurrent Version Control-MVCC
InnoDB is MVCC, is achieved by saving two hidden columns in the row behind each record, the two columns were saved this time to create and delete rows of time. Time here refers to system version number (can be understood as the transaction ID), each beginning a new transaction, the system will automatically increment the version number, system version number as the start time of the transaction ID of the transaction.
Lock
where the lock is divided into rows and table locks, as the name suggests, the line lock is a row lock, table lock is to lock the entire table. The difference between them is as follows:
particle size: Table lock> row locks
Efficiency: table lock> row locks
conflict: a table lock> row locks
concurrently: table lock <row lock
7. lock mode

As we mentioned in the lock, the lock lock plus what?

shared-lock
shared lock, the lock may also be referred to as read, multiple transactions can share the same lock, but only to read data.
Locking way: at the end of sql statement with lock in share mode.
Release the lock: commit / rollback.
exclusive-lock
exclusive lock can also be referred to as a write lock, it is a firm exclusive lock.
Locking manner: delete / insert / update operations InnoDB the default automatic locking, plus the end of the statement for update manually locked.
Release the lock: Same as above.
intention-shared-lock / intention- exclusive-lock
intent shared lock / intent exclusive lock, which is maintained by the database engine locks, the user can not operate. The lock can be understood as a mark, if you want to lock a table or a transaction to a lock range, then it needs to confirm the table or in the range of each row there are no other locks, the mark may be directly labeled the state of the entire table or range state, thereby avoiding a full table scan.
What is the lock?

Direct that conclusion, the index is locked, row lock to lock is the primary key index of a row. If a table is not indexed, the situation will lock the table, because there is a hidden column rowid, in the absence of an index, this column as a clustered index, leading to lock the table in index-organized tables.

8. Lock algorithm

record
point lock
unique index, precise matching
Gap
gap locks, lock range, not split infinite interval
Next-Key
integrated first two, lock range, right node comprising

Guess you like

Origin www.cnblogs.com/liaoweiming/p/12501382.html