当前位置:网站首页>ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
ACID Characteristics and Implementation Methods of MySQL Relational Database Transactions
2022-08-01 18:00:00 【Yisuyun】
ACID characteristics and implementation methods of MySQL relational database transactions
This article mainly introduces "MySQLRelational DatabaseACID characteristics and implementation methods of transactions", in daily operations, I believe many people have doubts about the ACID characteristics and implementation methods of MySQL relational database transactions.I hope it will be helpful for you to answer your doubts about "ACID characteristics and implementation methods of MySQL relational database transactions"!Next, please follow the editor to learn together!
1. ACID characteristics of transactions in detail
ACID are four properties that must be present in order to ensure that a transaction is correct and reliable:
Atomicity: Operations in a transaction succeed or fail simultaneously.
Consistency: Database transactions cannot destroy data integrity and business logic consistency.
Isolation: A transaction does not affect the operation of other transactions.
Durability: After the transaction is completed, the changes made by the transaction should be persisted in the database and will not be rolled back.
Take A transfer 100 yuan to B as an example:
Atomicity: A loses $100 at the same time as B receives $100.
Consistency: A's account cannot be negative after losing 100 yuan.
Isolation: If account A loses 1 yuan when executing B transaction while executing the transaction, it will eventually lose 101 yuan, and the two do not affect each other.
Persistence: After A's account loses 100 yuan, it cannot be recovered.
2. Implementation of MySQL transaction
MySQL transactions are implemented by the InnoDB storage engine.
A transaction can be started explicitly with the following command:
start transaction / (Begin);#One or more sql statements Commit;
In addition, in autocommit mode, each SQL statement we execute is a separate transaction; if autocommit mode is turned off, all SQL statements are in a transaction untilA commit or rollback is performed, the transaction ends, and another transaction begins.
The ACID properties of MySQL transactions are achieved by the following mechanisms:
Atomicity: undo log, logical log, records information related to SQL execution.When a rollback occurs, InnoDB will do the opposite according to the contents of the undo log
Persistence: redo log, when the transaction is committed, the fsync interface will be called to flush the redo log.
Isolation: Locking mechanism and MVCC.
Consistency: The design of the database itself.
3. Use of Gorm Transactions
Go's Gorm provides support for transactional operations:
db.Transaction(func(tx *gorm.DB) error { // Do some db operations in a transaction (from here you should use 'tx' instead of 'db') if err := tx.Create(&Animal{Name: "Giraffe"}).Error; err != nil { // Returning any error will rollback the transaction return err } if err := tx.Create(&Animal{Name:"Lion"}).Error; err != nil { return err } } // return nil Commit transaction return nil})In addition, there are operations such as nested transactions and manual transactions, you can refer to the Chinese document: Go GORM Transaction Details
4. Use of Spring Transactions
public class AClass { @Transactional(rollbackFor = Exception.class) public void aFunction() { //todo: database operation A (add, delete, this) }}The @Transactional annotation must be added to public methods. Private and protected methods are invalid.
In general, it is recommended to add the @Transactional annotation to the method, because @Transactional is directly added to the class or interface, and the @Transactional annotation will be valid for all public methods in the class or interface, which will affect performance.
At this point, the study on "ACID characteristics and implementation methods of MySQL relational database transactions" is over, and I hope to solve your doubts.The combination of theory and practice can better help everyone learn, go try it!If you want to continue to learn more relevant knowledge, please continue to pay attention to the Yisuyun website, and the editor will continue to work hard to bring you more useful articles!
边栏推荐
- 表达式;运算符,算子;取余计算;运算符优先顺序
- 频域分析实践介绍
- QLineEdit learning and use
- 存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
- 直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
- 【Day_08 0426】两种排序方法
- 【翻译】CNCF培养的OpenMetrics成为一个孵化项目
- Solve the problem that MySQL cannot insert Chinese data
- 快速抽取resnet_v2_152中间的特征层
- 【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
猜你喜欢
随机推荐
QLineEdit学习与使用
Leetcode73. 矩阵置零
关系运算符和if,else语句
【100个网络运维工作者必须知道的小知识!】
关于Mysql服务无法启动的问题
小贝拉机器人是朋友_普渡科技召开新品发布会,新一代送餐机器人“贝拉”温暖登场...
QT常用全局宏定义
云原生全景图详解
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) 题解
极化微波成像概述3
When custom annotations implement log printing, specific fields are blocked from printing
How to use the Golang coroutine scheduler scheduler
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
QT_QThread thread
理财产品的月年化收益率怎么算?
typora操作手册
bat 批示处理详解-2
QT_QThread线程
Review实战经典:2 种封装风格,你偏爱哪种?








