Skip to content

Support optimistic locking for PostgreSQL upserts in JDBC - #2430

Open
yeomin4242 wants to merge 4 commits into
spring-projects:mainfrom
yeomin4242:feature/2277-upsert-optimistic-locking
Open

yeomin4242 wants to merge 4 commits into
spring-projects:mainfrom
yeomin4242:feature/2277-upsert-optimistic-locking

Conversation

@yeomin4242

Copy link
Copy Markdown
Contributor

Related to #2277.

Add optimistic locking support to Spring Data JDBC upserts for PostgreSQL.

For entities with @Version, conflicting rows are updated only when the stored version matches the supplied version. Successful updates increment the version; mismatches raise OptimisticLockingFailureException.

New entities receive the usual initial version. If an entity already carries version n but no row exists, it is inserted with version n + 1.

This draft focuses on JDBC and PostgreSQL. Other built-in JDBC dialects reject versioned upserts with UnsupportedOperationException. Unversioned upsert behavior is retained.

Prepare initial and incremented versions for JDBC upserts and compare the previous version when updating a conflicting PostgreSQL row. Report optimistic locking failures without proceeding with referenced entity changes.

Add conditional conflict updates to the shared SQL model, retain unsupported-operation failures through strategy composition, and cover version initialization, conflicts, concurrency, immutable entities, and transactional aggregate rollback.

Related to spring-projects#2277.

Signed-off-by: YeongJae Min <whereismysejong@naver.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 14, 2026
Reuse the aggregate template integration suite and PostgreSQL fixture for versioned upserts. Preserve the concurrency, rollback, version and strategy-composition cases across both loading modes, and remove the duplicate unversioned upsert case.

Related to spring-projects#2277.

Signed-off-by: YeongJae Min <whereismysejong@naver.com>
Add the blank line required before the multi-statement constructor body.

Related to spring-projects#2277.

Signed-off-by: YeongJae Min <whereismysejong@naver.com>
Drop the deleted-row reinsertion test covered by absent-row insertion and remove the duplicate future-version mismatch value. Retain version initialization, conflicts, concurrency, referenced-entity preservation and rollback coverage.

Related to spring-projects#2277.

Signed-off-by: YeongJae Min <whereismysejong@naver.com>
@yeomin4242 yeomin4242 changed the title Add optimistic locking support for JDBC PostgreSQL upserts. Support optimistic locking for PostgreSQL upserts in JDBC Nov 14, 2026
@yeomin4242
yeomin4242 marked this pull request as ready for review November 14, 2026 12:19
@mp911de mp911de self-assigned this Nov 16, 2026
@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants