Skip to content

Replace CopyOnWriteArrayList with ArrayList in JdbcPagingItemReader and JpaPagingItemReader for performance and correctness #5112

@banseok1216

Description

@banseok1216

Bug description

JdbcPagingItemReader and JpaPagingItemReader create the internal results list as a CopyOnWriteArrayList:

if (results == null) {
    results = new CopyOnWriteArrayList<>();
} else {
    results.clear();
}

However, paging readers are strictly single-threaded.
AbstractPagingItemReader#doRead() already serializes access with an internal lock:

this.lock.lock();
try {
    ...
} finally {
    this.lock.unlock();
}

Because there is no concurrent access to results, using CopyOnWriteArrayList provides no benefit and introduces unnecessary write-cost overhead. A regular ArrayList is sufficient and more appropriate for this use case.

This is not a functional bug, but the current choice of collection does not match the actual access pattern of the readers.

Environment

  • Spring Batch version: 6.0.0
  • Java version: Java 21

Expected behavior
Since paging readers operate in a single-threaded, sequential manner, the internal list should use ArrayList instead of CopyOnWriteArrayList.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions