Description
As of v4.2.2, the javadocs of RepositoryItemWriter
state that the performance of the writer is determined by the performance of CrudRepository#saveAll
. However, the implementation does not call CrudRepository#saveAll
, but uses a for
loop in which the selected method is performed for each item.
This means that even if I want to use the saveAll
method by setting setMethodName("saveAll")
, the saveAll
method will be called for each item and not only once for all items. To use the saveAll
method, one needs to extend the writer and override doWrite
which is not convenient. Moreover, there is no validation that a method name is provided currently.
I understand that the motivation behind this "methodName" parameter is to allows users to select any method that might take a single item as a parameter and not a list (like update(item)
or save(item)
, etc), but I think it would be better to default to using saveAll
. In fact, the whole intent of the ItemWriter
concept in the first place is bulk updates, by making it operate on a list of items by design.
Using saveAll
instead of save
is 2x faster according to my first benchmark. I believe the cost of creating a MethodInvoker
and calling the method via reflection + not using a bulk operation is the root of this performance penalty.
My suggestion here is to use saveAll
by default (this will be consistent with Javadoc, which is not the case at the moment) and use the current behaviour if a method name is provided. Note that the RepositoryItemWriterBuilder
enforces that a method name is provided (which is a good point but not consistent with the writer that does not perform any validation), but this constraint can be relaxed to make things consistent and to benefit from the performance boost by default.