Using the environment

MySQL 8.0

Druid 1.1.21

A conclusion

When you use the @Transactional(readOnly = True) annotation ona service method, you can ensure that the same data cannot be repeatedly queried. If the annotation is not added, the data in the middle is modified during query A, and the modified data is read during query B. This phenomenon is called Non-repeatable read. For two transactions T1 and T2, T1 reads a field, T2 updates the field and commits it, T1 extracts the same field again, and the values are not the same.

Ii test Results

All theories need to be based on practice to be meaningful ~~~!!

1) Do not annotate @Transactional(readOnly = true)

Results:

Make sure to restart your project testing because Spring uses dynamic proxy, which means that the proxy classes are created dynamically when a project starts

Results:

The first query method is executed first

Then I manually modify the database

Execute the second query method

The result set will be the same as the first query method, so there will be no "unrepeatable reads", so you can add annotations according to the situation. In general, it is best to add this annotation when there is a combination of multiple queries