-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Panache#count()
leads to additional update
statements in Hibernate ORM
#35978
Comments
/cc @FroMage (panache), @loicmathieu (panache) |
This adds the HibernateHints.HINT_READ_ONLY to the `count()` calls. Named queries can specify the hint in the @NamedQuery annotation, so they don't need to be explicitly set. - Fixes quarkusio#35978
This adds the HibernateHints.HINT_READ_ONLY to the `count()` calls. `count()` named queries are only set when the hint is not set through the `@NamedQuery` annotation - Fixes quarkusio#35978
I don't think we have enough information to assume it's an ORM bug. What makes you suspect this? |
Because of what I said, I think:
Though I do agree we need to have a closer look as there could be weird configuration or mapping at play. I wonder if the reproducer in the description truly is enough to trigger this? There might be getters with side effects at play... ? |
@yrodiere what's the status of this? |
Needs investigation on the root cause, from what I can see. It's in the backlog, someone will have a look when they have time. |
FWIW, what I can see from the current implementations of I suspect there might be additional application logic (unrelated to the simple |
Already done recently: #41620
Right. I suspect that reproducer is not enough, but someone would need to have a look. |
I'm guessing you're referring to switching to the new Hibernate That is handled in a different place ( |
I think this issue was solved by someone else by adding WITH_HINT to true in another issue. But I can’t find the issue anymore. Anyways, the reason why the extra update statement was called, might have to do with:
This mapper is calling the getters of the entity to create and return a new dto, maybe that was causing it. But as I stated in my first post, as soon as I replace Panache#count with Panache#find with hints set to true, the update statement doesn’t happen. |
We should check whether the new |
@FroMage it doesn't, but as I already said Hibernate count APIs are not selecting any entity instance - thus not adding it to the persistence context, so it's not possible that an update be triggered from it. There must be something persisting the entity and causing it to be dirty in the user's application logic. |
I don't think we should do that; there are two possibilities regarding what was happening:
To me this looks like working as intended, unless we get a better reproducer clarifying details. Queries have no reason to be flagged explicitly read-only, ORM should do the "optimal thing TM". |
@Sanne How do you explain that the update statement doesn’t occur when you replace Panache#count with Panache#find with readonly set to true ? And I also don’t understand why the count method should not set the readonly hint to true by default. The reproducer is clear: After the persist call, a new dto is created which only calls the getters of the entity. |
In case of a count query, the table space might need to trigger an auto-flush; I'm not sure why that would happen in your specific case, but it might be necessary in some circumstances. In case of a find operation, such mechanism wouldn't be necessary to be triggered.
It shouldn't be necessary to make any difference in performance / efficiency. However forcefully disabling this w/o it being an explicit opt-in by the developer could cause problematic side effects, that's why I'm suggesting to not do that.
Fair enough, I didn't try the reproducer and I might be missing something but your other comments above seem to suggest it's not reproducing anymore? "I think this issue was solved by someone else ..." Also, several very capable colleagues looked already - could you clarify where we are and if there is a reproducer actually showing a problem? N.B. I'm not claiming that it's impossible for there to be an issue in Hibernate ORM, I'm just suggesting that if there's a bug in ORM we should fix it properly, while adding a hint in the Panache layer wouldn't be the best approach. We don't want to try to hide one bug by adding another bug in a different layer ;) But unless there's a working reproducer, we should close this. |
Panache#count()
leads to additional update
statements in Hibernate ORM
Hey @Serkan80 ,
I'm afraid the reproducer is incomplete, e.g. there's no mapping for the entities involved, so there's still work involved in creating an actual reproducer that someone can run to debug the problem. I also suspect that writing that reproducer will involve some other code, because the behavior you're witsnessing is very, very unpexected -- maybe I'm wrong. For now we'll be focusing on other issues that do have a working reproducer. If you have time to add one here, that would be great. Thank you. EDIT: To be clear, by "reproducer" we generally mean "a GitHub repository or ZIP with a full, but minimal, Quarkus application demonstrating the issue" |
Agreeing with @yrodiere and since I said approximately the same 2 weeks ago, I'll go ahead and close this. Feel free to reopen when a proper reproducer can show me wrong! ;) |
Describe the bug
When calling the count method on the PanacheEntity, the call is made with dirty checking (~statefull).
This results in unnecessary and unexpected extra SQL calls. And also causes errors, because we have an auditlog which is triggered via a db trigger, because it receives an insert & update record at the same time, which causes not uniqueness error on the primary keys.
See below for an example.
Expected behavior
The count call should be stateless, not resulting in an extra update call.
Expected sql calls should be:
Actual behavior
This results in the following queries:
How to Reproduce?
Output of
uname -a
orver
windows 10
Output of
java -version
JDK 17 Temurin
GraalVM version (if different from Java)
No response
Quarkus version or git rev
3.2.6.Final
Build tool (ie. output of
mvnw --version
orgradlew --version
)Maven 3.8.x
Additional information
A quick fix is by replacing the count with find:
It would be nice if the count() method was by default stateless (= with HINT_READONLY = true)
The text was updated successfully, but these errors were encountered: