Use AtomicUpdate to update account table

Otherwise updates may fail if two requests for the same account are
executed in parallel.

Bug: Issue 5721
Change-Id: Ic1a8a17d19982ff7a487ebe8aefe6a9f29baca1f
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-03-10 08:36:58 +01:00
parent 7c5103310b
commit 0457988491
5 changed files with 99 additions and 30 deletions

View File

@@ -27,12 +27,12 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.PutName.Input;
import com.google.gwtorm.server.AtomicUpdate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Collections;
@Singleton
public class PutName implements RestModifyView<AccountResource, Input> {
@@ -77,12 +77,23 @@ public class PutName implements RestModifyView<AccountResource, Input> {
throw new MethodNotAllowedException("realm does not allow editing name");
}
Account a = dbProvider.get().accounts().get(user.getAccountId());
String newName = input.name;
Account a =
dbProvider
.get()
.accounts()
.atomicUpdate(
user.getAccountId(),
new AtomicUpdate<Account>() {
@Override
public Account update(Account a) {
a.setFullName(newName);
return a;
}
});
if (a == null) {
throw new ResourceNotFoundException("account not found");
}
a.setFullName(input.name);
dbProvider.get().accounts().update(Collections.singleton(a));
byIdCache.evict(a.getId());
return Strings.isNullOrEmpty(a.getFullName())
? Response.<String>none()