I want to better the performance of a code here, I have this working:
List<Doctor> team = doctorService.getAll();
team.stream()
.filter(doctor -> !doctor.equals(msg.getSender()))
.forEach(doctor -> {
ChatMessageTeam chatMsg = new ChatMessageTeam();
chatMsg.setDoctor(doctor);
chatMsg.setMessage(msg);
rep.save(chatMsg);
});
The rep
is a JPA self-build repository.
And I want to parallel this stream, but when I put .parallel
on the stream, I got an error, as I saw because entity manager is not prepared to this. So I think (and not found out how) that I can do is accumulate the transactions and flush all them together in a single query then. Other suggestions are welcome too.
Thanks for your time reading this!