当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
以消费场景为驱动的CMDB要怎么建?
Go iota关键字与枚举类型实现原理是什么
研发团队数字化转型实践
数字化采购管理系统开发:精细化采购业务流程管理,赋能企业实现“阳光采购”
极化微波成像概述2
表达式;运算符,算子;取余计算;运算符优先顺序
MySQL关系型数据库事务的ACID特性与实现方法
基于ORB-SLAM2的改进代码
SRM供应商管理系统如何助力口腔护理企业实现采购战略的转型升级
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
我在启牛开户安全吗?谁能告诉我开不靠谱?
QLineEdit学习与使用
OpenCV安装、QT、VS配置项目设置
浅谈大数据背景下数据库安全保障体系
RecSys'22|CARCA:交叉注意力感知上下文和属性进行推荐
【Day_12 0507】二进制插入
【Day_12 0507】查找组成一个偶数最接近的两个素数
【Day_10 0428】密码强度等级
存储日报-数据湖架构权威指南(使用 Iceberg 和 MinIO)
移动端吸顶方案









