Skip to content

Cache query strings in SimpleJpaRepository to improve performance #3920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Dockerel
Copy link

@Dockerel Dockerel commented Jun 15, 2025

Optimizes SimpleJpaRepository by caching the query strings used for deleteAll and count operations.

  • Introduced final fields to cache the deleteAll and count query strings during repository construction.
  • Updated usages in deleteAllInBatch() and count() to use the cached query strings instead of generating them on every call.

Previously, the query strings for deleteAll and count were generated on each method invocation, causing repeated operations and unnecessary object creation. By caching these strings, we reduce redundant computation and improve performance, especially in applications with frequent batch delete or count operations.

The change would be safe as the query strings are based on immutable entity metadata and do not change during the repository's lifecycle.

Please review if there are scenarios where the entity metadata might change after repository construction, which could affect the correctness of the cached queries.

Let me know if you have any concerns or suggestions for further improvement!

Dockerel added 2 commits June 15, 2025 16:54
Cache the deleteAll and count query strings as final fields in SimpleJpaRepository.
This avoids repeated String.format operations and reduces unnecessary object creation
on every invocation of deleteAllInBatch() and count(). No functional changes.

Signed-off-by: Giheon Do <[email protected]>
Signed-off-by: Giheon Do <[email protected]>
@Dockerel Dockerel force-pushed the cache-query-strings branch from b30acf7 to 397ec35 Compare June 15, 2025 07:54
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 15, 2025
@mp911de mp911de self-assigned this Jun 16, 2025
@mp911de mp911de added type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: task A general task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants