Transactions in a MYSQL database

Transaction: Transaction is a concept in "database", which means that "a business" in business processing corresponds to multiple operations in the database.

Transactions_Transactions in MySQL:

a> The default transaction processing method in MySQL: automatic submission -- treat each sql statement as an independent transaction, and the data will be changed immediately after execution. It is not related to other sql statements and cannot meet the needs of transaction processing.

b> View the current transaction processing method of mysql:

          show variables like 'autocommit';

c> There are two ways to handle transactions in mysql

1> Turn off automatic submission and change to "manual submission"

                    set autocommit =off

                    execute sql statement

                    commit/rollback

                    commit or rollback

Note: After committing or rolling back, all previous SQL statements are processed, either all changed to the database, or all new SQL statements are written after canceling them, which is a new transaction

Once committed, it cannot be rolled back; once rolled back, it cannot be committed again.

2> In the "auto-commit" state, temporarily open a "manual transaction"

                    start transaction starts a temporary transaction

                    sql statement

                    commit or rollback

                   Note: As long as the transaction is committed or rolled back, the transaction is over and immediately restores to the previous automatic transaction state.

                              Transactions are session-level for MySQL, and each session can be set up for transaction processing independently of each other and isolated from each other

                              Query results in a session can find the results of previous modifications, regardless of commit/rollback

                              But in another session, the uncommitted data of a transaction cannot be read (mysql's default isolation level). Transaction_transaction characteristics :

 1. Atomicity Atomicity means that a transaction is an indivisible unit of work, and the operations in the transaction either all occur or none of them occur.
 2. Consistency The integrity of the data before and after the transaction must be consistent.
 3. Isolation (Isolation) The isolation of a transaction means that when multiple users access the database concurrently, the transaction of one user cannot be interfered by the transactions of other users, and the data between multiple concurrent transactions must be isolated from each other.
 4. Durability Durability means that once a transaction is committed, its changes to the data in the database are permanent, and even if the database fails, it should not have any effect on it.

Transaction_Problems that arise if the isolation of two transactions is not considered:

 1. Dirty read: A transaction reads uncommitted data of another transaction.
 2. Non-repeatable read: A transaction reads data that has been committed (update) by another transaction. Initiates another transaction, and multiple queries in the transaction result in inconsistent results.
 3. Phantom read/phantom read: A transaction reads data that another transaction has committed (insert). Causes another transaction with inconsistent results from multiple queries within a transaction.

TRANSACTION_TRANSACTION ISOLATION LEVEL:

 1). On the database side, the degree of mutual influence between two transactions can be determined by "setting the isolation level of the transaction":
 2). Four isolation levels:

 1).read uncommitted: read uncommitted; lowest level; basically not isolated. Dirty data can be read.
 2).read committed: read has been committed; dirty reads can be resolved, but "non-repeatable reads" and "phantom reads" cannot be resolved;
 3).repeatable read: repeatable reads. It can be solved under MYSQL: dirty reading, non-repeatable, phantom reading; (MySQL's default level)
 4).serializable: Completely isolate two transactions, when one transaction is not over, the other transaction must wait;

 3). View the isolation level of the current database:
  select @@tx_isolation;
 4). Set the transaction isolation level of the session:

  set session transaction isolation level One of the four isolation levels above;
 5). Security and performance comparison
  security: serializable > repeatable read > read committed > read uncommitted
  performance: serializable < repeatable read < read committed < read uncommitted
 6). Common The default isolation level of the database:
  MySql: repeatable read (third level)
  Oracle: read committed (second level)

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326094391&siteId=291194637