当前位置:网站首页>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!
边栏推荐
- 阿里云的域名和ip绑定
- 公用函数----mfc
- MySQL 45 讲 | 09 普通索引和唯一索引,应该怎么选择?
- SQL的索引详细介绍
- Leetcode75. Color Classification
- How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
- 2022年SQL大厂高频实战面试题(详细解析)
- 史上最全的Redis基础+进阶项目实战总结笔记
- 深入分析类加载器
- 完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
猜你喜欢
随机推荐
【Day_09 0427】 另类加法
广汽埃安“弹匣电池”,四大核心技术,出行安全保障
【Day_11 0506】 最近公共祖先
关于MySql中explain结果filtered的理解
How can become a good architect necessary skills: painting for all the people praise the system architecture diagram?What is the secret?Quick to open this article and have a look!.
云原生全景图详解
变量交换;复合赋值;增递减运算符
浅谈大数据背景下数据库安全保障体系
GRUB2的零日漏洞补丁现已推出
opencv real-time face detection
【100个网络运维工作者必须知道的小知识!】
When custom annotations implement log printing, specific fields are blocked from printing
8月微软技术课程,欢迎参与
opencv基本的图像处理
【Day_10 0428】井字棋
【Day_08 0426】求最小公倍数
顺序表的简单描述及代码的简单实现
公用函数----mfc
网上开户佣金万一靠谱吗,网上开户安全吗
typora操作手册









