MySQL learning record transaction

Dad can't stand it 2020-11-06 21:30:07
mysql learning record transaction


  • What is business

Business is a series of DML( Additions and deletions ) The execution of the statement is successful , Or all failed . Once executed successfully, it will exist forever .

  • Why business is needed

for example : The amount of the card is 1000 element , Li Si's bank card amount is 1500 element . At this time, if Li Si transfers money to Zhang San 200 element , Suppose we divide the transfer of money here into 2 strip sql perform , They are Li Si's amount -200 The amount of yuan and Zhang San +200 element . First carry out Li Si's amount -200 Yuan this sql And it worked , Then execute Zhang San's amount +200 Yuan this sql But it failed , At this time, Li Si's amount became 1300, But Zhang San's amount has not changed . We don't allow this to happen , This is when we need to use transactions .

  • Four characteristics of transactions

Atomicity : Either all statements are executed and submitted successfully , Either all statements fail to execute and roll back . Partial success is not allowed 、 Part of the failure .

Uniformity : When the transaction completes , The state of the database must be consistent , Is the database from a normal state to another normal state .

Isolation, : When multiple users operate on the same data at the same time , There will be multiple transactions , Every transaction is independent , One transaction does not affect another .

persistence : Once the transaction is committed successfully , Then the data will persist , Even if the system sends a fault .

  • The isolation level of the transaction

Read uncommitted (Read uncommitted) When a transaction begins to write to a row of data , Other transactions cannot write to the row data at the same time , But it can read . Transactions cannot operate the same data at the same time to avoid update confusion , But it's time to read data , Once a transaction is rolled back, dirty reads are created .

for example :account Tabular balance The original value is 500,A Business balance Modified into 300 But the transaction is not committed ,B The transaction comes to read this at this time balance Value reads 300,B After the transaction is read A Transactions need to be rolled back for some unusual reasons ,balance It's back to 500, This is the time B Transaction read 300 Namely Dirty reading

Read submitted (Read committed) When a transaction reads data , Another transaction can write , However, when the write operation is in progress, other transactions can no longer read and write . This avoids dirty reading , But it will cause It can't be read repeatedly

for example :A Transaction read first account Of balance The value of is 500, At this time, other transactions can be written ,B The business is A After reading the transaction for the first time balance Changed to 300,B After the transaction write operation is completed A Read the business again balance It will read balance=300, With the first reading of balance=500 atypism , And that's what happened It can't be read repeatedly

Repeatable (Repeatable read) When a transaction is not finished reading and writing data , Other transactions are not allowed to read or write the data . This avoids dirty reading and non repeatable reading, but at the same time it causes Fantasy reading

for example :A Business needs to put account The whole of the watch balance Modified into 0, When A When the transaction is executed B Business to account Table inserts a balance=300 The data of , After two transactions are completed, it may be found that not all of them balance All changed to 0, There will be one balance=300 The data of , This is it. Fantasy reading

Serializable (Serializable) Each transaction must be queued, executed one by one , Two or more transactions are not allowed at the same time . It completely avoids any problems caused by transaction parallelism .

Safety factor : Serializable > Repeatable degrees > Read submitted > Read uncommitted

Coefficient of performance : Read uncommitted > Read submitted > Repeatable > Serial number

It can be seen that the relationship between safety and performance of isolation level is inversely proportional , because The principle of isolation level is through locking or serialization

MySql Have the above four isolation levels , The default isolation level is repeatability (Repeatable read). and Orcale There are only two isolation levels, read committed and serializable , The default isolation level is read committed (Read committed)

  • Redo log And Undo log

Because of the concept of transactions , So when we operate the data, we do not simply operate the data in the disk directly , It will also involve Undo log Files and Redo log file , For example, we make a update When operating, it will record Undo log as well as Redo log, When we need to roll back data , We need to use the undo log Roll back to ensure atomicity , When we need to persist data, we need to use Redo log To ensure durability

Redo log principle : To ensure durability , Every time you're done sql Records to Redo log, Downtime occurs while data is still in memory , Can pass Redo log To do persistence ,Redo log It's execution sql It is then recorded and persisted to disk before the transaction is committed .

Undo log principle : To ensure atomicity , Every time you execute one sql There was always a record of the opposite sql( For example, execute a clause inserts sentence ,Undo log I'll record one delete sentence ), When the data needs to be rolled back, it can be done through Undo log To complete ,Undo log It's execution sql And q Previous record .

Let's use the example of Zhang San Li Si's transfer at the beginning to sort out the process , Zhang San's balance=1000, Li Si's balance=1500, Li Si transfers money to Zhang San 200

 

Three characteristics of transactions in the learning process ( Atomicity , Isolation, , persistence ) There are traces to follow , Only consistency has no relevant principle information , And there are many ambiguities . As I understand it , Probably the principle of consistency is atomicity 、 Isolation and persistence , Their ultimate goal is to keep the database in a normal state

版权声明
本文为[Dad can't stand it]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云