【发布时间】:2018-10-01 13:34:54
【问题描述】:
基本上就是标题所说的。我们有一些将在同一个函数中读取和更新的关键数据,我们必须确保我们可以避免竞争条件。 @Transactional 注释会解决这个问题吗?
// Both threads call this function
void someMethod() {
int value = EntityObject.getSomeField();
int newValue = modifyValue(value);
// PROBLEM: The other thread read "someField" before the database was updated,
// and we end up with the wrong value when both threads are done
EntityObject.setSomeField(newValue);
EntityObjectService.save(EntityObject);
}
我们正在使用 MySQL
【问题讨论】:
-
请发minimal reproducible example 并在您的问题中添加其他必要的详细信息,例如您正在访问的数据库类型。
标签: java spring hibernate tomcat jpa