当前位置:网站首页>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!
边栏推荐
猜你喜欢
解决MySQL插入不了中文数据问题
Leetcode74. 搜索二维矩阵
opencv如何实现图像倾斜校正
研发团队数字化转型实践
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!.
不需要写代码,快速批量修改文件夹中图片的格式
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
hcip第九天
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
今年最火爆的词:商业分析,看这一篇就够了!
随机推荐
QT基础功能,信号、槽
GRUB2的零日漏洞补丁现已推出
Detailed explanation of the working principle of crystal oscillator
tooltip 控件
QLineEdit学习与使用
Leetcode73. 矩阵置零
广汽埃安“弹匣电池”,四大核心技术,出行安全保障
我在启牛开户安全吗?谁能告诉我开不靠谱?
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
【Day_11 0506】 最近公共祖先
塔防海岸线用户协议
2022年SQL大厂高频实战面试题(详细解析)
tooltip control
粒子滤波 particle filter —从贝叶斯滤波到粒子滤波——Part-I(贝叶斯滤波)
sql添加索引
Leetcode72. 编辑距离
力扣每日一题-第45天-697. 数组的度
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
关于单应性矩阵的若干思考
关于LocalDateTime的全局返回时间带“T“的时间格式处理