Does Spring think it failed after Mysql commits the transaction?

 

I. Introduction

       Recently, someone raised a question when using the blogger's Redis rollback tool. This tool rolls back according to the exceptions in the transaction process, so as to ensure the consistency between DB and redis.

       So if Mysql succeeds when the transaction is committed, but Spring thinks it is not successful and throws an exception, and the redis rollback tool fails at this time, then it becomes that mysql has data, but redis has no data?

       This student obviously doesn't understand the transactional interaction between spring and mysql.

 2. Analysis

  1. First, let’s take a look at how the problem he thought happened. He may not be very clear about the process of this problem. Ask him why mysql completed the spring and didn’t know how to throw an exception. He is obviously a little confused, but Figure 1 The drawing is clearer, and one step is missing

d8b9ce443a5f4342a6a6edd4d8db7356.png

 

 

2. One less step is that mysql execution should give spring a response packet. If it is lost in the network process, then spring will self-check beyond the time and throw "java.sql.SQLTimeoutException", but he thinks it is spring throwing After this exception occurs, the mysql transaction has been completed, so it will not be rolled back.

85d438af1c9e4fc5bd648f6b506d6470.png

 

 

3. Here we need to mention how spring transactions interact with mysql. In "Mysql Technology Insider: Innodb Storage Engine", the rollback after starting a transaction is actually using the savepoint technology that comes with mysql, which is equivalent to holding A mark that rolls back to the data image where the mark is located when needed, and the data image is saved in the undo log

8d8a442e2d3f4cf491bc0f87dea38d27.png

 

 

4. After the transaction is committed, the flat transaction can still be rolled back to any save point. The chain transaction is actually related to the broadcast mode in the spring annotation. According to the principle, its setting is actually very simple, so I won’t describe it here.

45ee119aa6c74c16b8ab41cb52757f29.png

 

 

5. Going back to the original question, does Spring think it is unsuccessful after Mysql submits? The answer is yes, but after spring throws an exception, does mysql not roll back because the transaction has been submitted? If the answer is no, it can still be saved by Click to roll back, so there is no problem mentioned by this student.

Four. Summary

        Considering the abnormal situation needs to be combined with many principles, so as to know whether it will happen. This is mainly the understanding and analysis of the spring framework and "Mysql Technology Insider: Innodb Storage Engine". Of course, it does not mean that the blogger's analysis is completely correct, there are differences Opinions welcome to communicate in the comment area.

 

Guess you like

Origin blog.csdn.net/m0_69270256/article/details/130430823