Solve the problem of "echo modification" when concurrently modifying, the later modification will overwrite the previous problem.

method one,

By adding the version field to the database, when the "echo modification" operation is performed when solving concurrency,

When different users modify the same data at the same time, the later modification will overwrite the previous modification.

 

A description of the situation in which the problem occurred and an overview of the problem resolution:

Source file link for this screenshot: Youdao Cloud Notes

Some notes:

1. The transaction has been added, why can't it be solved, and the version field needs to be added?

The solution to concurrency by transactions is mainly the isolation level of transactions (uncommitted read, committed read, repeatable read, serialization). It solves the concurrency problem when a transaction is executed. But the echo modification is not in a transaction.

2. Is this completely resolved?

No, adding the version field here only solves the problem of echoing to the form and executing if(newVersion == oldversion) after submission. When both operations go into the if(newVersion == oldversion) { } block. After submitting, the problem occurs again.

But we know that the probability of problems in concurrency is related to the time to perform an operation. In this problem, "the time when the user views the form and clicks to modify", and "the program executes after the program enters this if code block. Time" is clearly taking the lead (90%).

That is to say, adding version solved our big problem.

3. How to solve it completely?

Very simple, lock this code modification method on the basis of using version. Ensuring that only one person can commit changes at a time solves the rest of the problem.

 

4. Is there any other method?

have! (The sample code below has limitations)

Method Two,

That is, when you submit an update, only the items you modified will be updated. This way it won't overwrite someone else's stuff.

1) Put the original echo object into the session.

2) Get the new object after submitting the modification, and the old object placed in the session, and pass in the service

3) For comparison in the service layer, the items that have not been changed are set to null, and the changed ones are reserved.

Compare different tool codes

4. Through dynamic sql (mybatis) in the mapper layer, it can be updated as needed.

<update id="updateTargetPhone" parameterType="TargetPhone">
	UPDATE tb_target_phone
	<set>
      <if test="phone.name != null">
      	name = #{phone.name},
      </if>
      <if test="phone.remainMoney != null">
      	remain_money = #{phone.remainMoney},
      </if>
      <if test="phone.smsSend != null">
      	sms_send = #{phone.smsSend}
      </if>
    </set>
  	WHERE id = #{phone.id}
  </update>

 

Guess you like

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