Skip to content

Added ModelIllegalStateException to handle lazy loading exception.#9761

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

Added ModelIllegalStateException to handle lazy loading exception.#9761
hmlnarik merged 1 commit intokeycloak:mainfrom
ahus1:9645-exception-handling

Conversation

@ahus1
Copy link
Copy Markdown
Member

@ahus1 ahus1 commented Jan 24, 2022

Closes #9645

@ahus1
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 24, 2022

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 Indicates an issue that touches storage (change in data layout or data manipulation) 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
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 25, 2022

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.

Copy link
Copy Markdown
Contributor

@vramik vramik left a comment

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.

Copy link
Copy Markdown
Contributor

@vramik vramik left a comment

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
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 26, 2022

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
Comment thread services/src/main/java/org/keycloak/exportimport/util/ExportUtils.java Outdated
@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
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 28, 2022

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

@ahus1 ahus1 requested a review from hmlnarik January 28, 2022 15:09
Comment thread services/src/main/java/org/keycloak/exportimport/util/ExportUtils.java Outdated
@ahus1 ahus1 force-pushed the 9645-exception-handling branch from 403a739 to a08e753 Compare January 28, 2022 15:22
Comment thread server-spi/src/main/java/org/keycloak/models/ModelIllegalStateException.java Outdated
Comment thread services/src/main/java/org/keycloak/exportimport/util/ExportUtils.java Outdated
Comment thread services/src/main/java/org/keycloak/services/resources/admin/ClientsResource.java Outdated
Comment thread services/src/main/java/org/keycloak/services/managers/ResourceAdminManager.java Outdated
@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
Copy link
Copy Markdown
Member Author

ahus1 commented Jan 31, 2022

@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!

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.

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

Labels

area/storage Indicates an issue that touches storage (change in data layout or data manipulation) 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