


This article brings you relevant knowledge about mysql. It mainly introduces the ACID characteristics of MySQL transactions and concurrency problem solutions. The article provides a detailed introduction around the topic, which has certain reference value. , friends who need it can refer to it, I hope it will be helpful to everyone.
Recommended learning: mysql video tutorial
1. The concept of transaction
A transaction is An indivisible unit composed of one or more SQL statements that operate on the database. Only when all operations in the transaction are executed normally, the entire transaction will be submitted to the database. If some transaction processing fails, then the transaction will be To roll back to the original state , therefore, either all transactions succeed or all fail.
So we need to remember a few basic concepts of transactions, as follows:
A transaction is the execution of a set of SQL statements, either all succeed or all fail, and no part of it can appear Success, partial failure results, atomic operations that guarantee transaction execution. Only when all SQL statements of the transaction are successfully executed can the transaction be committed and the results written to the disk. During transaction execution, if some SQL errors occur, the transaction must be rolled back to the original state.
For example, the transfer business requires multiple SQL statements to be completed together. Only when these SQL statements are executed successfully can the business be considered successful.
Transaction processing has three states:
begin: Open all sql statements to be executed in a transaction All are successful, and then commit commits a transaction. If any of the SQL statements causes an abnormal SQL execution due to a power outage or server error, the transaction will not be submitted and the transaction will be rolled back (rollback), the data will be restored to the state before the transaction started
This is guaranteed by the storage engine (guaranteed by redo log and undo log)
MyISAM storage engine does not support transactions, while InnoDB storage engine supports transactions and row locks.
Use show engines\G
to check which storage engines are supported by the current database.
select @@autocommit;
View settings for transaction commit status
Database engine It can be modified temporarily through commands or permanently through configuration files.
If our business involves transactions, we usually control this variable in code. Generally speaking, our transactions consist of multiple SQLs and must satisfy the atomic operation of the transaction, so we set it to Submit manually. If the business is successful, the transaction will be submitted; if there is a failure in the middle of the business, one transaction will be rolled back.
2. ACID characteristics
Every transaction must meet the following 4 characteristics:
Atomicity of transactions(Atomic): A transaction is an indivisible whole. The transaction must have atomic characteristics, and when the transaction is modified, either all will be executed or none will be executed, that is, the completion of the transaction part is not allowed. Transaction Consistency(Consistency):Before and after a transaction is executed, the database data must maintain a consistent state. The consistency state of the database must be the responsibility of the user and implemented by the concurrency control mechanism. Take online shopping as an example. Only by letting the goods leave the warehouse and entering the customer's shopping cart can a complete transaction be completed. (Consistency is not only reflected in transactions, but also includes the introduction of MySQL in the storage layer. In order to improve the access efficiency of hotspot data, a cache layer Redis or Memery cache is generally added to cache the hotspot data, which involves the data of the cache layer and the database DB layer. Consistency issue) Transaction Isolation(Isolution): When two or more transactions are executed concurrently, in order to ensure data security, the operations within one transaction are isolated from the operations of other transactions. Not visible to other executing transactions, prevents concurrently executing transactions from affecting each other. Isolation level: data security and transaction concurrency. The stricter the isolation, the higher the security, and the lower the concurrency (that is, concurrency control, ensuring data security) Durability (Durability) of the transaction: After the transaction is completed (transaction commit is successful), the DBMS guarantees Its modifications to the data in the database are permanent. Even if the database fails due to a failure, the data should be able to be restored.
The most important thing about MySQL is the log, not the data!
The ACD feature of transactions is guaranteed by MySQL's redo log and undo log mechanisms; I isolation is guaranteed by the lock mechanism of mysql transactions.
3. Problems with transaction concurrencyTransaction processing is not isolated, and the following problems usually occur when executing transactions concurrently:
Dirty Read: One transaction reads uncommitted data from another transaction. For example, when transaction A and transaction B are executed concurrently, after transaction A is updated, transaction B queries and reads A's uncommitted data. At this time, transaction A rolls back, and the data read by transaction B is invalid dirty data (Transaction B read the uncommitted data of transaction A)Non-repeatable read(NonRepeatable Read): The operation of one transaction caused another transaction to read different data twice before and after. For example, when transaction A and transaction B are executed concurrently, after transaction B queries and reads data, transaction A updates the data queried by transaction B. At this time, transaction B reads the data again and finds that the data read twice are different. Same. (Transaction B read the submitted data of transaction A)Phantom Read(Phantom Read)Phantom Read: The operation of one transaction results in the amount of data resulting from two queries before and after another transaction different. For example, when transaction A and transaction B are executed concurrently, after transaction B queries and reads data, transaction A adds or deletes a record that meets the query conditions of transaction B. At this time, transaction B queries again and finds that the previous query does not Existing records, or some records from the previous query are missing. (Transaction B has read the newly added data of transaction A or cannot read the data deleted by transaction A)
Dirty reading must be eliminated because the transaction is not committed. In some scenarios, non-repeatable reads and phantom reads are allowed (the transaction has been committed), but do not necessarily have to be eliminated (by setting different isolation levels), which is determined by the application scenario requirements.
4. Transaction related commandsselect @@autocommit;Check whether MySQL automatically commits transactions
0 means manual submission of transactions, 1 means automatic submission of transactions, set the transaction submission method to manual submission (only affects the current session):
The above is the detailed content of Summary of ACID characteristics and concurrency issues of MySQL transactions. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
