How to ensure data consistency

1. Declarative transactions. @Transcation

   ---- Problem: A large number of operations in a function will lead to a long lock time, especially when there are third-party operations in the middle, which will lead to response timeout, or the database thread pool will be occupied.

2. Programmatically make the transaction TranscationTemplate

    And use optimistic locking with version number.

            The version field is added to the table to indicate the version.

    First make sure that the version value of the record to be modified is 100

       In the execution method of TranscationTemplate, execute the SQL statement: UPDATE TABLE SET COL1='', VERSION=VERSION+1 WHERE COL2='' AND VERSION = 100;

      And get the return value, that is, the number of modified bars.

        If the number of bars is 1, it means that the modification is successful, that is, you can go down.

        If the number of bars is not 1, it means that the modification failed, that is, it is impossible to go down.

         ----- Problem, there is no way to roll back.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325688003&siteId=291194637