最近写的MySQL业务逻辑常常遇到
com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Lock wait timeout exc
异常报错,(这业务逻辑是先查询、再update、再逻辑删除,期间sql的条件语句用到了where xxx in ()
),于是去学习MySQL事务相关的东西。研究过程中不可避免的是常常遇到数据库设计原则ACID。因此笔者去MySQL官方文档看了下关于ACID原文介绍。
参考自:MySQL官方文档——InnoDB存储引擎之ACID模型
本篇博客会出现大量的英文文档,因为是从MySQL官方文档粘贴过来的,目的是为了将最官方的概念解释放出来,避免由于主观原因误解某些概念。如果对英文有一定阅读程度的小伙伴可直接跳过英文直接看中文。
直接去MySQL官方文档查看,原文介绍如下:
The ACID model is a set of database design principles that emphasize aspects of reliability that are important for business data and mission-critical applications. MySQL includes components such as the InnoDB storage engine that adhere closely to the ACID model so that data is not corrupted and results are not distorted by exceptional conditions such as software crashes and hardware malfunctions. When you rely on ACID-compliant features, you do not need to reinvent the wheel of consistency checking and crash recovery mechanisms. In cases where you have additional software safeguards, ultra-reliable hardware, or an application that can tolerate a small amount of data loss or inconsistency, you can adjust MySQL settings to trade some of the ACID reliability for greater performance or throughput.
笔者结合自己的个人理解以及谷歌翻译将其翻译成中文,直接拿去有道翻译或谷歌翻译会容易曲解或者难以理解其中的意思。笔者的翻译如下:
ACID模型是一个数据库设计原则的集合,说白了ACID模型是一个规则集合,里面有若干个规则作用于数据库,即这些规则要求数据库怎么样去处理一件事。这些规则使数据库的 可用性[1] 更加突出。因此可以认为ACID的目的、作用就是为了保证数据库的可用性。这个可用性特性对业务数据以及具有关键任务的应用非常重要。MySQL包含的组件中,比如InnoDB存储引擎会严格地遵循ACID模型,数据库存储的数据不会毁坏、结果不会因异常情况(比如软件崩溃、硬件故障)而失真。当你依赖符合ACID的特性时,你无需重新造一致性检查以及故障恢复机制的轮子。如果你有额外的软件措施、超可靠的硬件或者可容忍少量数据丢失或数据不一致的应用程序,你可以调整MySQL的配置用部分的可用性换取高性能或者高吞吐量的效果。
【注解】:
[1]:可用性可以大概理解为高可用,高可用与高性能常常不可兼得,想要高性能,必要时肯定要放弃某些规则,这就有可能导致可用性降低。
总结: ACID是一个规则集合,这些规则目的是保证数据库的可用性、数据库存储的数据不会毁坏、结果不会因异常情况(比如软件崩溃、硬件故障)而失真。
官方原文介绍如下:
The following sections discuss how MySQL features, in particular the InnoDB storage engine, interact with the categories of the ACID model:
A: atomicity.
C: consistency.
I: isolation.
D: durability.
笔者结合个人理解以及谷歌翻译将其翻译成中文,翻译如下:
以下部分讨论MySQL的特性,特别是InnoDB引擎是如何与ACID的类别交互的:
A:原子性
C:一致性
I:隔离性
D:持久性
官方原文介绍:
transaction
Transactions are atomic units of work that can be committed or rolled back. When a transaction makes multiple changes to the database, either all the changes succeed when the transaction is committed, or all the changes are undone when the transaction is rolled back.Database transactions, as implemented by InnoDB, have properties that are collectively known by the acronym ACID, for atomicity, consistency, isolation, and durability.
笔者翻译如下:
事务在提交(commit)或者回滚(roll back)时是一个原子工作单元。当事务对数据库产生改变时,这些改变要么在提交的时候全部成功;要么全部回滚,撤销这些改变。InnoDB存储引擎的数据库事务的实现,常常被统称为ACID,它们分别是atomicity(原子性)、consistency(一致性)、isolation(隔离性)、durability(持久性)的首字母缩写。
官方文档介绍:
Isolation
The isolation aspect of the ACID model mainly involves InnoDB transactions, in particular the isolation level that applies to each transaction. Related MySQL features include:
The autocommit setting.
Transaction isolation levels and the SET TRANSACTION statement. See Section 15.7.2.1, “Transaction Isolation Levels”.
The low-level details of InnoDB locking. Details can be viewed in the INFORMATION_SCHEMA tables (see Section 15.15.2, “InnoDB INFORMATION_SCHEMA Transaction and Locking Information”) and Performance Schema data_locks and data_lock_waits tables.
笔者的翻译:
ACID模型的隔离级别(Isolation)特性主要涉及到InnoDB的事务,特别是 应用于每个事务的隔离级别。相关的MySQL特性包括:
- 自动提交设置
- 事务的隔离级别以及
set transaction
语句。详情见第15.7.2.1节,“事务隔离级别”- InnoDB锁的低级细节。详情可以参考
information_schema
表以及从data_locks
、data_lock_waits
表查看性能策略。
官方文档介绍:
isolation level
One of the foundations of database processing. Isolation is the I in the acronym ACID; the isolation level is the setting that fine-tunes the balance between performance and reliability, consistency, and reproducibility of results when multiple transactions are making changes and performing queries at the same time.
From highest amount of consistency and protection to the least, the isolation levels supported by InnoDB are: SERIALIZABLE, REPEATABLE READ, READ COMMITTED, and READ UNCOMMITTED.
With InnoDB tables, many users can keep the default isolation level (REPEATABLE READ) for all operations. Expert users might choose the READ COMMITTED level as they push the boundaries of scalability with OLTP processing, or during data warehousing operations where minor inconsistencies do not affect the aggregate results of large amounts of data. The levels on the edges (SERIALIZABLE and READ UNCOMMITTED) change the processing behavior to such an extent that they are rarely used.
笔者翻译如下:
隔离性是数据库处理过程的基础之一。隔离性是
ACID
缩写中的I
;当多个事务同时对数据库做改变或者做查询时,隔离级别是性能、可用性、一致性和结果的可再现性的微平衡设置。
InnoDB支持的隔离级别有SERIALIZABLE
(串行)、REPEATABLE READ
(可重复读)、READ COMMITTED
(读已提交)、READ UNCOMMITTED
(读未提交)。它们的一致性从高到低排序,也就是它们的并发时的性能从低到高。
很多用户对InnoDB表的操作,都是使用默认的隔离级别REPEATABLE READ
(可重复读)。专家们可能选择READ COMMITTED
(读已提交),因为它们使用OLTP
[1] 处理来推动可扩展性的界限,或者在数据仓库操作期间,轻微的不一致不会影响大量数据的聚合结果。边缘的级别(SERIALIZABLE
和READ UNCOMMITTED
)将处理行为改变到很少使用的程度。
[1]:“在线交易处理”的 缩写。一种数据库系统或数据库应用程序,它运行具有许多事务的工作负载,频繁写入和读取,通常一次影响少量数据。例如,航空公司预订系统或处理银行存款的应用程序。为了在DML (插入/更新/删除)效率和 查询效率之间取得平衡,可能会以规范化的形式组织数据 。对比 数据仓库。
凭借其行级锁定和 事务功能, InnoDB是 OLTP 应用程序中使用的 MySQL 表的理想存储引擎。