Skip to content

GH-11558: DefaultLockRepository: ON CONFLICT DO NOTHING on PostgreSQL - #11559

Open
Adrastopoulos wants to merge 1 commit into
spring-projects:mainfrom
Adrastopoulos:GH-11558
Open

Adrastopoulos wants to merge 1 commit into
spring-projects:mainfrom
Adrastopoulos:GH-11558

Conversation

@Adrastopoulos

@Adrastopoulos Adrastopoulos commented Oct 18, 2026 •

Copy link
Copy Markdown

Fixes #11558

afterPropertiesSet() reads the database vendor the way JdbcMetadataStore does, and appends ON CONFLICT DO NOTHING to the default insert query on PostgreSQL. A query set via setInsertQuery() is used as is, so the documented getInsertQuery() + " ON CONFLICT DO NOTHING" doesn't get the clause twice. acquire() already treats 0 inserted rows as not acquired.

PostgresLockRepositoryTests checks that a contended acquire() returns false and that the server logs no int_lock_pk violation; it fails without the change.

A database-agnostic alternative is the conditional insert that JdbcMetadataStore uses for putIfAbsent() (INSERT ... SELECT ... HAVING COUNT(*)=0), with no vendor check. It changes the default SQL on every database and the parameters of a custom insert query, so it seems to fit a major version better. Happy to switch to it if you prefer.

…HING` on PostgreSQL

Fixes: spring-projects#11558

On PostgreSQL, every `acquire()` that loses the race for an existing lock
fails its `INSERT` on `INT_LOCK_PK`, and the server logs
`duplicate key value violates unique constraint` each time.
A `LockRegistryLeaderInitiator` candidate does that about once a second.

* Append `ON CONFLICT DO NOTHING` to the default insert query on PostgreSQL,
as `JdbcMetadataStore` already does for its `putIfAbsent()` query
* Leave a query set via `setInsertQuery()` as is, so the documented
`getInsertQuery() + " ON CONFLICT DO NOTHING"` does not get the clause twice
* Document the new default
* Add `PostgresLockRepositoryTests` for a contended `acquire()`

Signed-off-by: Gabriel Hall <gabriel.hall@cox.net>

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No complains.
Will be merged next week.

Thank you!

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DefaultLockRepository: add ON CONFLICT DO NOTHING on PostgreSQL by default

2 participants