Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the invalid execution of update statement in mybatis

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "invalid execution of update statements in mybatis how to solve", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to solve the invalid execution of update statements in mybatis"!

Sometimes the execution of mybatis update statements in a project is invalid.

During the test, the company tester found that in the points system, consumption generates points, but sometimes it cannot be added to the user's cumulative points.

Obviously the integration pipelining record and the increase of user points are in the same transaction. The integral pipelining record is generated successfully, but the user integral is not added? That's weird.

The code for adding points is:

TMemberPointMapper.updateByPrimaryKeySelective (tMemberPoint)

Then, in the corresponding location, add a log to record the comparison before and after the update statement.

The tester checked the log when he found the problem again, and the log showed that the object after the execution of the update statement did accumulate points. Why don't the values in the database tables change?

So Baidu, ah, the result of this situation is less, some say it is the problem of the version of mybatis? Personally, I don't think it's possible, because this mybatis version is used in many projects and hasn't gone wrong. The transaction is also tested, and there are exceptions that can be rolled back.

Then he calmed down and thought about the timing of the problem. Immediately after the consumption operation, the tester will click on the interface to view the user's points. At this point, the interface for obtaining user credit information will be called. Because this interface is not just a query, because of the rating maintenance mechanism (after a certain period, part of the points should be deducted), it has the operation to modify the user's points before the query. The interface for querying user credits is also in a non-read-only transaction.

When two transactions that record operations on the same user are in charge at the same time (row-level lock of InnoDb, exclusive lock), because the default isolation level of mysql is repeatable-read, transaction An and transaction B, after reading the data as a, transaction B modifies the data to b and commits, and transaction A performs the modification operation, it will change the data b to data a. This gives rise to the occasional false appearance that the execution of the update statement is invalid as described in the question.

Solution.

Pessimistic lock adds for update after the query statement, locks the transaction, and queries the statement of user points.

After select distinct from t_member_point order by ${orderByClause} for update executes the update statement, the data is not updated and no error is reported.

Record a problem you encountered today:

Problem description

After the update statement is executed, the data is not updated and no error is reported

Details

A sql statement printed on the console by passing parameters; copying the sql statement to the database for execution is also OK

Solution.

It's hard to understand. What exactly is the problem?

So, I tried a lot of methods and finally found a problem with a certain field; but from the sql statement printed on the console, the parameter value is also OK.

Then I try to write the parameters of the sql statement in mapper as the parameters printed by the console, directly determine it, run it, and find that it is also OK, so I'm sure it's the parameter problem.

However, there is no problem with mapping or anything, so the problem may lie in the length of this field in the data inventory. Finally, it is found that the length of this field in the database is longer than the value of the passed parameter, and the attribute value is processed with trim () in the entity class, so the problem is here. Just modify it.

Thank you for your reading, the above is the content of "how to solve the invalid execution of update statements in mybatis". After the study of this article, I believe you have a deeper understanding of how to solve the problem of invalid execution of update statements in mybatis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 209

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report