Open ZetalyAlexandre opened 3 months ago
/cc @DavideD (hibernate-reactive), @FroMage (panache), @gavinking (hibernate-reactive), @loicmathieu (panache)
Yes, this is a limitation we have at the moment. The Hibernate session does not support concurrent calls. We mention this in the Hibernate Reactive documentation: https://staging.hibernate.org/reactive/documentation/2.3/reference/html_single/#_sessions_and_vert_x_contexts
On the backend, we do everything necessary to prevent this from happening. But when the calls come from multiple external calls (customer calls via the REST api), how can I avoid this kind of anomaly?
You should have a different session for each request. Quarkus should take care of it.
What stopping you from adding a new access point in FruitResource
?
@DELETE
public Uni<Response> deleteAll() {
return Panache.withTransaction( () -> Panache.executeUpdate( "delete from Fruit" )
.map( deleted -> Response
.ok( "Number of fruits deleted: " + deleted )
.build() ) );
}
PS: You should be able to write this:
@DELETE
public Uni<Response> deleteAll() {
return Panache.withTransaction(Fruit::deleteAll)
.map(deleted -> Response.ok("Number of fruits deleted: " + deleted).build());
}
But I've found this bug: https://github.com/quarkusio/quarkus/issues/41967
I've done more test and I do not have this issue when I try to mass update or mass create. So it's very much linked to delete only. Nothing prevent me to add a "mass" delete endpoint. We've already found a workaround for our particular application. Unit deletions by several users are rare in our application, so this shouldn't be a problem for the time being.
I was just afraid that this case could also arise with mass creation or modification, which doesn't seem to be the case.
It seems that I do not have the issue if I replace:
@DELETE
@Path("{id}")
public Uni<Response> delete(Long id) {
return Panache.withTransaction(() -> Fruit.deleteById(id))
.map(deleted -> deleted
? Response.ok().status(NO_CONTENT).build()
: Response.ok().status(NOT_FOUND).build());
}
by
@DELETE
@Path("{id}")
public Uni<Response> delete(Long id) {
return Panache.withTransaction(() -> Panache.executeUpdate("delete from Fruit where id=?1", id))
.map(deleted -> deleted > 0
? Response.ok().status(NO_CONTENT).build()
: Response.ok().status(NOT_FOUND).build());
}
If the session is not re-created each time, it can definitely happen in all situations, even if you don't see any error now. Simply because the Session is not designed to be used in parallel.
Maybe I was a bit too quick in looking at this issue, though. I will have a second look.
That said, I think that adding a new Rest point to FruitResource
is a better solution because it will keep all the actual logic in one place.
OK, nevermind, it doesn't seem like it's using the same session, so it's something else that's causing the error
I was just afraid that this case could also arise with mass creation or modification, which doesn't seem to be the case.
I suppose it could happen, I don't see why this should be deletion specific. The error is related to when the session is open/used. Sadly, I don't have the time right now to look into this.
The same error happens using hibernate reactive without Panache, so it could be a bug in Quarkus or Hibernate Reactive. I will have another look at it as soon as I have time, if nobody else is going to work on it.
I'm also not familiar with AngularJS in this situation, maybe somebody else can chime in?
This is very strange, as I've done the following tests (one call per create/update/delete, all done with concurrency):
This only happens when using “Fruit.deleteById(id)” with only 5 simultaneous requests, but has never happened for the others...
I'll let you work on it. Thanks for your responsiveness.
Hello,
@DavideD Do you think it's related to this old Zulip discussion ? https://quarkusio.zulipchat.com/#narrow/stream/187030-users/topic/Reactive.20exception.20.22Detected.20use.20of.20the.20reactive.2E.2E.22/near/380321402
That's why I had to downgrade in 2.x in the past. I didn't want to take any chances, despite all our tests before going live. However, this problem is very important to me because it happened again today...
I'm not sure, but thanks for reminding me. I will check for sure.
Can you give me some details about your use case?
The Hibernate ORM upgrades are taking me longer than I've anticipated, but I hope I can get back to check these bugs soon
I had a similar problem, same error but while inserting with ~20 concurrent request with about 1000k records each within 5 entities
I analyzed a heap dump with VisualVM and found that one instance of ReactiveSessionImpl
was still open even though there was no activity at that time. Looking a bit more I found this code that seams a bit sneaky (singleton with an instance variable holding a session..). So I disabled hibernate.order_inserts
that I had activated it previously for 20% speed gain and I d'ont have this issue anymore
@DavideD this looks like a good code suspect, no?
Could be. As soon as I have finished something else I will have a better look
Describe the bug
Concurrent calls to the same api performing a delete in the database sometimes cause an anomaly “HR000069: Detected use of the reactive Session from a different Thread”. All calls are on different elements. This problem does not occur when requests are sequential.
Stacktrace example:
Expected behavior
No exceptions should be thrown, all calls should work.
Actual behavior
An exception is sometimes raised with the following error message: “HR000069: Detected use of the reactive Session from a different Thread”.
How to Reproduce?
Project to reproduce: : hibernate-reactive-panache-quickstart.zip
I used the hibernate-reactive-panache-quickstart example. I've only added a “Remove all” button to the index.html file. This makes a call to the delete api, starting from the fruit list. There are no changes in the quarkus part.
JS function added:
Launch the project, access the page and use the “Remove all” option. The number of errors may vary
Output of
uname -a
orver
No response
Output of
java -version
openjdk version "17.0.9" 2023-10-17
Quarkus version or git rev
3.2.12.Final
Build tool (ie. output of
mvnw --version
orgradlew --version
)Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Additional information
No response