8000 Added ModelIllegalStateException to handle lazy loading exception. by ahus1 · Pull Request #9761 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Added ModelIllegalStateException to handle lazy loading exception. - #9761

Merged
hmlnarik merged 1 commit into
keycloak:mainfrom
ahus1:9645-exception-handling
Jan 31, 2022
Merged

hmlnarik merged 1 commit into
keycloak:mainfrom
ahus1:9645-exception-handling

Conversation

@ahus1
@ahus1 ahus1 commented Jan 24, 2022
Copy link
Copy Markdown
Member

Closes #9645

@ahus1
ahus1 commented Jan 24, 2022 •
Copy link
Copy Markdown
Member Author

This makes sure that the updated delegate exists in the database and that its optimistic locking version hasn't changed.

After this change the ConcurrencyTest with JPA Map storage completes.

Design changes:

  • common base class for all JpaDelegateProviders to write the code only once. Pushing up the delegate instance variable. To enable the comparison interface JpaRootEntity introduced
  • ClientService will filter out invalid clients as before.
  • Interface didn't match well for logoutAll(), therefore therefore catching the exception here - ideas welcome
  • ExportUtils will fail if a client can't be exported ... this changes the existing behavior, but seems to be reasonable as exportutils should export everything, and no clients shuold be missing.

Optional stuff (please comment):

  • Entity manager could be pushed to JpaDelegateProvider as well.

Will need to do the same changes in #9746 (Client Scope for JPA Map) as well.

@ahus1
ahus1 requested a review from vramik January 24, 2022 16:41
@ahus1 ahus1 added area/storage Storage layer — JPA entities, database schemas, Liquibase migrations, caching keycloak.x kind/bug Categorizes a PR related to a bug labels Jan 24, 2022
@ahus1
ahus1 marked this pull request as draft January 24, 2022 17:59
@ahus1
ahus1 commented Jan 25, 2022
Copy link
Copy Markdown
Member Author

Testing this yesterday, 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).

I added a second commit to this PR to keep them separate for an initial review and comment. After initial feedback on the approach I'll then merge them.

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"

@vramik, @sguilhen, @hmlnarik - I'd be happy if you could have a look.

@vramik vramik left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @ahus1. On the first look it seems good to me. I haven't tried it yet.

I've noticed one thing (please see inline) what attracted my attention. Could you please in the meantime take a look while I'll take a closer look. Thank you.

@vramik vramik left a comment •
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I've looked mostly on jpa part and from my POV it looks good. I've added few minor comments.

Thank you @ahus1.

@ahus1
ahus1 force-pushed the 9645-exception-handling branch 2 times, most recently from 36876e5 to e258df3 Compare January 26, 2022 13:59
@ahus1
ahus1 commented Jan 26, 2022
Copy link
Copy Markdown
Member Author

Thanks @vramik for the findings, I update the PR with once commit that contains all of your changes. To make it simpler for @sguilhen and @hmlnarik to review, I kept the commits separate. That should make it simpler to decide if both steps should be taken.

@ahus1
ahus1 requested review from hmlnarik and sguilhen January 26, 2022 14:05
@ahus1
ahus1 force-pushed the 9645-exception-handling branch from e258df3 to 91b4772 Compare January 26, 2022 15:27
@ahus1
ahus1 force-pushed the 9645-exception-handling branch from 91b4772 to bdaac6b Compare January 28, 2022 15:00
@ahus1
ahus1 marked this pull request as ready for review January 28, 2022 15:04
@ahus1
ahus1 commented Jan 28, 2022 •
Copy link
Copy Markdown
Member Author

After a conversation with @hmlnarik the changes about the hibernate listener described in #9761 (comment) are no longer part of this ticket; they will be covered in a future ticket. Work on that will start once this has been merged.

Previous commit still available here: 4c0bcee21d106a8451b89ec94c122bf75ac0d8e8

7438
@ahus1
ahus1 requested a review from hmlnarik January 28, 2022 15:09
@ahus1
ahus1 force-pushed the 9645-exception-handling branch from 403a739 to a08e753 Compare January 28, 2022 15:22
@ahus1
ahus1 force-pushed the 9645-exception-handling branch from a08e753 to 0acd011 Compare January 31, 2022 07:42
@ahus1
ahus1 requested a review from sguilhen January 31, 2022 07:43
@ahus1
ahus1 commented Jan 31, 2022
Copy link
Copy Markdown
Member Author

@sguilhen - Thanks for the review. I updated this PR according to your review comments. Please have another go if this is good to go. Thanks!

@hmlnarik hmlnarik left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the PR!

@hmlnarik
hmlnarik merged commit df7ddbf into keycloak:main Jan 31, 2022
@ahus1
ahus1 deleted the 9645-exception-handling branch January 31, 2022 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
6BB6

Labels

area/storage Storage layer — JPA entities, database schemas, Liquibase migrations, caching kind/bug Categorizes a PR related to a bug

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

Handling lazy loading exceptions for storage in new and old storage

4 participants

0