Management Services

The basic flow of transaction:

Open transaction: start transaction;

Creating a save point: savepoint named Save

Back to save point (depending on the circumstances): rollback to save roll call

 

On the MySQL specific presentation:

MySQL > Start Transaction ; - Open Services 

Query the OK, 0 rows affected ( 0.00 sec) 

MySQL > that the savepoint AA; - set a savepoint AA 

Query the OK, 0 rows affected ( 0.00 sec) 

MySQL >  INSERT  INTO Account values ( . 1 , ' Zhang ' , 10 ); - add a record 

Query the OK, . 1 Row affected ( 0.00 sec) 

MySQL >BB that the savepoint; - set a savepoint BB 

Query the OK, 0 rows affected ( 0.00 sec) 

MySQL >  INSERT  INTO Account values ( 2 , ' John Doe ' , 10000 ); - add a record 

Query the OK, . 1 Row affected ( 0.00 sec) 

MySQL >  the SELECT  *  from the Account; - two records in the 

+ - - + -------- + ---------- + 

| the above mentioned id | name | Balance| 

+ - - + -------- + ---------- + 

|  1  | Joe Smith |  10.00  | 

|  2  | John Doe |  10000.00  | 

+ - - + - + ---------- + ------- 

2 rows in  the SET ( 0.00 sec) 

MySQL >  ROLLBACK  to bb; - found that a record is added later misuse. Therefore, the rollback state bb 

Query the OK, 0 rows affected ( 0.01 sec) 

MySQL >  SELECT  *  from Account; - second record no

+ - - + -------- + --------- + 

| the above mentioned id | name | Balance | 

+ - - + -------- + --- + ------ 

|  1  | Joe Smith |  10.00  | 

+ - - + -------- + --------- + 

1 Row in  the SET ( 0.03 sec)

Precautions practical operation:

1, if no transaction savepoints, can also be rolled back, only to begin to roll back the transaction, the direct use rollback (provided that the transaction has not been submitted)

2, if a transaction is submitted to (commit), it can not be rolled back (rollback)

3, deliberately choose which storage node fallback

4, InnoDB supports transactions, MyISAM does not support transactions

5, beginning with the start transaction transaction

 

Transaction isolation level:

When we have multiple clients operating on a particular table in the database, how to isolate the operation? MySQL provides a level of isolation

When this happens, MySQL provides a mechanism that allows different transaction data when in operation, with isolation, to ensure data consistency

Let's look at what is called "dirty read."

"Dirty read" refers to when a transaction is accessing data, and the data has been modified, this change has not yet committed to the database, then another transaction also access this data and then use this data

For example:

  • 1.Mary original wage of 1000, financial officers will pay instead of Mary 8000 (but did not submit the transaction)
  • 2.Mary read their wages, they found their wages into a 8000, full of joy!
  • 3. The financial found a mistake, the transaction is rolled back, Mary's wages into a 1000 like this, Mary wages remember the number 8000 is a dirty

 

Add version
optimistic locking lock-free, Client A read v1, v2 modified after its set, Client B read v2, v1 is not the same as before (because you want to change it would have been v2 is the client B) in order to distinguish
only increase unabated, as long as it was found a large update than the original version, we need to consider carefully

 

ACID properties of a transaction

Atomicity (Atomicity):

Application transaction is the smallest unit of execution, as is the atomic nature of the smallest particles, having the same characteristics can not be divided, the transaction applications is not subdivided smallest logical execution bodies.

Consistency (Consistency):

Result of the execution of transactions, it must make the database from one consistent state, change to another consistent state. When the database contains only the outcome of the transaction successfully committed, the database is in a consistent state. If the system is running an interrupt occurs, a transaction was interrupted while not yet complete, but changed incomplete transactions have been written to the database modifications made to the database, then the database will be in a incorrect (inconsistent) state. Consistency is therefore ensured by atomicity.

Isolation (Isolation):

Execution without disturbing each individual transaction, a transaction of any internal operations to other concurrent transactions are isolated. In other words, you can not see the other side of the intermediate state between transactions to execute concurrently, and can not influence each other concurrently executing transactions.

Persistent (Durability):

Persistence means that once a transaction is committed, it changes made to the database records to be permanently stored therein (such as: disk).

 

Guess you like

Origin www.cnblogs.com/hetaoyuan/p/11404450.html