1
0
mirror of https://github.com/google/nomulus synced 2025-12-23 06:15:42 +00:00

Change nested transact calls to retransact (#2563)

This commit is contained in:
Weimin Yu
2024-09-20 11:16:52 -04:00
committed by GitHub
parent e167b4b753
commit 4ba0f4a2cd
9 changed files with 31 additions and 29 deletions

View File

@@ -177,8 +177,9 @@ public class AllocationTokenFlowUtils {
return maybeTokenEntity.get(); return maybeTokenEntity.get();
} }
// TODO(b/368069206): `reTransact` needed by tests only.
maybeTokenEntity = maybeTokenEntity =
tm().transact(() -> tm().loadByKeyIfPresent(VKey.create(AllocationToken.class, token))); tm().reTransact(() -> tm().loadByKeyIfPresent(VKey.create(AllocationToken.class, token)));
if (maybeTokenEntity.isEmpty()) { if (maybeTokenEntity.isEmpty()) {
throw new InvalidAllocationTokenException(); throw new InvalidAllocationTokenException();

View File

@@ -345,7 +345,7 @@ public final class EppResourceUtils {
"key must be either VKey<Contact> or VKey<Host>, but it is %s", "key must be either VKey<Contact> or VKey<Host>, but it is %s",
key); key);
boolean isContactKey = key.getKind().equals(Contact.class); boolean isContactKey = key.getKind().equals(Contact.class);
return tm().transact( return tm().reTransact(
() -> { () -> {
Query query; Query query;
if (isContactKey) { if (isContactKey) {

View File

@@ -82,7 +82,7 @@ public class FeatureFlag extends ImmutableObject implements Buildable {
TimedTransitionProperty.withInitialValue(FeatureStatus.INACTIVE); TimedTransitionProperty.withInitialValue(FeatureStatus.INACTIVE);
public static Optional<FeatureFlag> getUncached(FeatureName featureName) { public static Optional<FeatureFlag> getUncached(FeatureName featureName) {
return tm().transact(() -> tm().loadByKeyIfPresent(createVKey(featureName))); return tm().reTransact(() -> tm().loadByKeyIfPresent(createVKey(featureName)));
} }
public static ImmutableList<FeatureFlag> getAllUncached() { public static ImmutableList<FeatureFlag> getAllUncached() {

View File

@@ -579,7 +579,7 @@ public class DomainBase extends EppResource
/** Loads and returns the fully qualified host names of all linked nameservers. */ /** Loads and returns the fully qualified host names of all linked nameservers. */
public ImmutableSortedSet<String> loadNameserverHostNames() { public ImmutableSortedSet<String> loadNameserverHostNames() {
return tm().transact( return tm().reTransact(
() -> () ->
tm().loadByKeys(getNameservers()).values().stream() tm().loadByKeys(getNameservers()).values().stream()
.map(Host::getHostName) .map(Host::getHostName)

View File

@@ -1014,7 +1014,7 @@ public class RegistrarBase extends UpdateAutoTimestampEntity implements Buildabl
/** Loads and returns a registrar entity by its id directly from the database. */ /** Loads and returns a registrar entity by its id directly from the database. */
public static Optional<Registrar> loadByRegistrarId(String registrarId) { public static Optional<Registrar> loadByRegistrarId(String registrarId) {
checkArgument(!Strings.isNullOrEmpty(registrarId), "registrarId must be specified"); checkArgument(!Strings.isNullOrEmpty(registrarId), "registrarId must be specified");
return tm().transact(() -> tm().loadByKeyIfPresent(createVKey(registrarId))); return tm().reTransact(() -> tm().loadByKeyIfPresent(createVKey(registrarId)));
} }
/** /**

View File

@@ -172,19 +172,16 @@ public class RegistrarPocBase extends ImmutableObject implements Jsonifiable, Un
*/ */
public static void updateContacts( public static void updateContacts(
final Registrar registrar, final ImmutableSet<RegistrarPoc> contacts) { final Registrar registrar, final ImmutableSet<RegistrarPoc> contacts) {
tm().transact( ImmutableSet<String> emailAddressesToKeep =
() -> { contacts.stream().map(RegistrarPoc::getEmailAddress).collect(toImmutableSet());
ImmutableSet<String> emailAddressesToKeep = tm().query(
contacts.stream().map(RegistrarPoc::getEmailAddress).collect(toImmutableSet()); "DELETE FROM RegistrarPoc WHERE registrarId = :registrarId AND "
tm().query( + "emailAddress NOT IN :emailAddressesToKeep")
"DELETE FROM RegistrarPoc WHERE registrarId = :registrarId AND " .setParameter("registrarId", registrar.getRegistrarId())
+ "emailAddress NOT IN :emailAddressesToKeep") .setParameter("emailAddressesToKeep", emailAddressesToKeep)
.setParameter("registrarId", registrar.getRegistrarId()) .executeUpdate();
.setParameter("emailAddressesToKeep", emailAddressesToKeep)
.executeUpdate();
tm().putAll(contacts); tm().putAll(contacts);
});
} }
public String getName() { public String getName() {

View File

@@ -118,7 +118,7 @@ public class HistoryEntryDao {
/** Loads all history objects from all time from the given registrars. */ /** Loads all history objects from all time from the given registrars. */
public static Iterable<HistoryEntry> loadHistoryObjectsByRegistrars( public static Iterable<HistoryEntry> loadHistoryObjectsByRegistrars(
ImmutableCollection<String> registrarIds) { ImmutableCollection<String> registrarIds) {
return tm().transact( return tm().reTransact(
() -> () ->
Streams.concat( Streams.concat(
loadHistoryObjectByRegistrarsInternal(ContactHistory.class, registrarIds), loadHistoryObjectByRegistrarsInternal(ContactHistory.class, registrarIds),

View File

@@ -39,7 +39,8 @@ public final class TmchCrl extends CrossTldSingleton {
/** Returns the singleton instance of this entity, without memoization. */ /** Returns the singleton instance of this entity, without memoization. */
public static Optional<TmchCrl> get() { public static Optional<TmchCrl> get() {
return tm().transact(() -> tm().loadSingleton(TmchCrl.class)); // TODO(b/368069206): move transaction up to `TmchCertificateAuthority#updateCrl`
return tm().reTransact(() -> tm().loadSingleton(TmchCrl.class));
} }
/** /**

View File

@@ -19,6 +19,7 @@ import static google.registry.model.registrar.RegistrarPocBase.Type.ABUSE;
import static google.registry.model.registrar.RegistrarPocBase.Type.ADMIN; import static google.registry.model.registrar.RegistrarPocBase.Type.ADMIN;
import static google.registry.model.registrar.RegistrarPocBase.Type.TECH; import static google.registry.model.registrar.RegistrarPocBase.Type.TECH;
import static google.registry.model.registrar.RegistrarPocBase.Type.WHOIS; import static google.registry.model.registrar.RegistrarPocBase.Type.WHOIS;
import static google.registry.persistence.transaction.TransactionManagerFactory.tm;
import static google.registry.testing.DatabaseHelper.loadRegistrar; import static google.registry.testing.DatabaseHelper.loadRegistrar;
import static google.registry.testing.DatabaseHelper.persistResource; import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.testing.DatabaseHelper.persistSimpleResource; import static google.registry.testing.DatabaseHelper.persistSimpleResource;
@@ -49,16 +50,18 @@ class RegistrarPocCommandTest extends CommandTestCase<RegistrarPocCommand> {
@Test @Test
void testList() throws Exception { void testList() throws Exception {
Registrar registrar = loadRegistrar("NewRegistrar"); Registrar registrar = loadRegistrar("NewRegistrar");
RegistrarPoc.updateContacts( tm().transact(
registrar, () ->
ImmutableSet.of( RegistrarPoc.updateContacts(
new RegistrarPoc.Builder() registrar,
.setRegistrar(registrar) ImmutableSet.of(
.setName("John Doe") new RegistrarPoc.Builder()
.setEmailAddress("john.doe@example.com") .setRegistrar(registrar)
.setTypes(ImmutableSet.of(ADMIN)) .setName("John Doe")
.setVisibleInWhoisAsAdmin(true) .setEmailAddress("john.doe@example.com")
.build())); .setTypes(ImmutableSet.of(ADMIN))
.setVisibleInWhoisAsAdmin(true)
.build())));
runCommandForced("--mode=LIST", "--output=" + output, "NewRegistrar"); runCommandForced("--mode=LIST", "--output=" + output, "NewRegistrar");
assertThat(Files.readAllLines(Paths.get(output), UTF_8)) assertThat(Files.readAllLines(Paths.get(output), UTF_8))
.containsExactly( .containsExactly(