Skip to content

Ensure that parent's version ID is incremented when an attribute changes#9894

Merged
hmlnarik merged 1 commit intokeycloak:mainfrom
ahus1:9874-parent-version-increment
Feb 1, 2022
Merged

Ensure that parent's version ID is incremented when an attribute changes#9894
hmlnarik merged 1 commit intokeycloak:mainfrom
ahus1:9874-parent-version-increment

Conversation

@ahus1
Copy link
Copy Markdown
Member

@ahus1 ahus1 commented Jan 31, 2022

This is necessary to allow the optimistic locking functionality to work as expected when changing only attributes on an entity.

Closes #9874

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

Testing this, I found that the optimistic locking failed when updating only some attributes: the version in the parent wasn't incremented as the the ManyToOne relation is owned by the child.

I looked for a simple solution to change that ownership, but didn't succeed. I then added some hibernate listeners and iterfaces to trigger the essential session.lock(root, LockModeType.OPTIMISTIC_FORCE_INCREMENT).

The approach is inspired by https://vladmihalcea.com/jpa-entity-version-property-hibernate/ and https://vladmihalcea.com/hibernate-event-listeners/.

I tested it manually by changing role attribute or changing a client's "Backchannel Logout Session Required" and running it with and without session.lock(root, LockModeType.OPTIMISTIC_FORCE_INCREMENT);.

@ahus1 ahus1 added area/storage Indicates an issue that touches storage (change in data layout or data manipulation) keycloak.x labels Jan 31, 2022
@ahus1 ahus1 requested review from sguilhen and vramik January 31, 2022 12:04
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

Will be reworked to supported multiple levels. Reverting to draft.

@ahus1 ahus1 marked this pull request as draft January 31, 2022 12:51
@ahus1 ahus1 assigned ahus1 and unassigned hmlnarik Jan 31, 2022
@ahus1 ahus1 removed request for sguilhen and vramik January 31, 2022 13:04
…ges.

This is necessary to allow the optimistic locking functionality to work as expected when changing only attributes on an entity.

Closes keycloak#9874
@ahus1 ahus1 force-pushed the 9874-parent-version-increment branch from 14c455b to b627e46 Compare January 31, 2022 13:21
@ahus1 ahus1 assigned hmlnarik and unassigned ahus1 Jan 31, 2022
@ahus1 ahus1 requested review from sguilhen and vramik and removed request for vramik January 31, 2022 13:22
@ahus1 ahus1 marked this pull request as ready for review January 31, 2022 13:22
@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

Has been reworked to supported multiple levels of parent-child-relationship. Ready for review.

Copy link
Copy Markdown
Contributor

@sguilhen sguilhen left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @ahus1 !

Copy link
Copy Markdown
Contributor

@hmlnarik hmlnarik left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

@hmlnarik hmlnarik merged commit 9d46b45 into keycloak:main Feb 1, 2022
@ahus1 ahus1 deleted the 9874-parent-version-increment branch February 2, 2022 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/storage Indicates an issue that touches storage (change in data layout or data manipulation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JPA Map storage doesn't increment version column on attribute update

3 participants