Aller au contenu

Write Inside Loop (ARCH4052)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.orm.write-inside-loop · Severity error · Category orm

Calling save/flush on every loop iteration produces a separate DB round-trip per record. Accumulate the writes and flush once outside the loop.

for (User user : users) {
user.setActive(true);
userRepository.save(user); // flush each iteration -> N round-trips
}
for (User user : users) {
user.setActive(true);
}
userRepository.saveAll(users); // single batch

Move the persist call out of the loop and apply it once via saveAll/batch flush.