I'm working on a Spring application and I need to update every user in a database (around 4500 records).
My code works well in my local environment with a few users, but I'm afraid in the testing environments and even in live it will be extremly slow if I get every user from the database.
Here is my code so far:
public void syncUsers() {
List<User> usersList = userService.getAllUsers();
usersList.stream().forEach((user) -> {
if (isNull(user.getSocialId())) {
String id = socialRestAPIService.syncContacts(user);
user.setSocialId(id);
userService.updateUser(user);
}
});
}
This one is the userService.getAllUsers
method:
public List<User> getAllUsers () {
return userRepository.findAll();
}
What I want to achive is update 100 users in one time and then update the next 100 so on... How is it possible?
If the userRepository
is a JpaRepository
then it implements the PagingAndSortingRepository
which has a method
findAll(Pageable pageable).
Hence, you can create a Pageable
like this:
Pageable.ofSize(100).withPage(10)