-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Panache and Hibernate Reactive use different Vert.x local contexts #47441
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
Draft
DavideD
wants to merge
4
commits into
quarkusio:main
Choose a base branch
from
DavideD:47314-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...ests/hibernate-reactive-panache/src/main/java/io/quarkus/it/panache/reactive/Counter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.it.panache.reactive; | ||
|
||
import io.quarkus.hibernate.reactive.panache.PanacheEntityBase; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
|
||
@Entity | ||
public class Counter extends PanacheEntityBase { | ||
|
||
@Id | ||
private Long id; | ||
private int count = 0; | ||
|
||
public Counter() { | ||
} | ||
|
||
public Counter(long id) { | ||
this.id = id; | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public int getCount() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(count); | ||
} | ||
|
||
public void increase() { | ||
this.count++; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...tive-panache/src/main/java/io/quarkus/it/panache/reactive/WithTransactionCounterBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.quarkus.it.panache.reactive; | ||
|
||
import org.hibernate.reactive.mutiny.Mutiny; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
import io.quarkus.hibernate.reactive.panache.Panache; | ||
import io.quarkus.hibernate.reactive.panache.common.WithTransaction; | ||
import io.smallrye.mutiny.Uni; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import static jakarta.persistence.LockModeType.PESSIMISTIC_WRITE; | ||
|
||
/** | ||
* The goal if this class is to test the usage of {@link WithTransaction}, | ||
* you should not use {@link io.quarkus.hibernate.reactive.panache.Panache#withTransaction(Supplier)} | ||
* nor {@link org.hibernate.reactive.mutiny.Mutiny.SessionFactory#withTransaction(Function)} | ||
*/ | ||
@ApplicationScoped | ||
@WithTransaction | ||
public class WithTransactionCounterBean { | ||
|
||
private static final Long ID_COUNTER = 42L; | ||
|
||
@Inject | ||
Mutiny.SessionFactory sessionFactory; | ||
|
||
public Uni<Counter> createOrResetCounter() { | ||
final Counter counter = new Counter(ID_COUNTER); | ||
return sessionFactory.withStatelessSession(session -> session | ||
.upsert(counter) | ||
.replaceWith(counter) | ||
); | ||
} | ||
|
||
public Uni<Counter> increaseCounterWithHR() { | ||
return sessionFactory.withSession(session -> session | ||
.find(Counter.class, ID_COUNTER, PESSIMISTIC_WRITE) | ||
.invoke(Counter::increase) | ||
); | ||
} | ||
|
||
public Uni<Counter> increaseCounterWithPanache() { | ||
return Counter | ||
.<Counter> findById(ID_COUNTER, PESSIMISTIC_WRITE) | ||
.invoke(Counter::increase); | ||
} | ||
|
||
public Uni<Void> assertThatSessionsAreEqual() { | ||
return sessionFactory.withSession(hrSession -> Panache | ||
.getSession().chain(panacheSession -> { | ||
if (panacheSession != hrSession) { | ||
return Uni.createFrom().failure(new AssertionError("Sessions are different!")); | ||
} | ||
return Uni.createFrom().voidItem(); | ||
}) | ||
); | ||
} | ||
|
||
public Uni<Counter> findCounter() { | ||
return sessionFactory.withSession(session -> session | ||
.find(Counter.class, ID_COUNTER) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...te-reactive-panache/src/test/java/io/quarkus/it/panache/reactive/WithTransactionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.quarkus.it.panache.reactive; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.smallrye.mutiny.Uni; | ||
import jakarta.inject.Inject; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import static io.quarkus.vertx.VertxContextSupport.subscribeAndAwait; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
/** | ||
* Hibernate Reactive and Panache store the created sessions in a local Vert.x context, | ||
* and we need to make sure that they won't get lost. | ||
* See issue <a href="https://github.com/quarkusio/quarkus/issues/47314">47314</a> | ||
* | ||
* @see io.quarkus.hibernate.reactive.panache.common.runtime.SessionOperations | ||
* @see io.quarkus.hibernate.reactive.panache.common.WithTransaction | ||
*/ | ||
@QuarkusTest | ||
public class WithTransactionTest { | ||
// How many times we want to increase the counter | ||
private static final int INCREASES_NUM = 10; | ||
|
||
@Inject | ||
WithTransactionCounterBean counterBean; | ||
|
||
@BeforeEach | ||
public void createOrResetCounter() throws Throwable { | ||
subscribeAndAwait(counterBean::createOrResetCounter); | ||
} | ||
|
||
@Test | ||
void increaseCounterWithHibernateReactive() throws Throwable { | ||
subscribeAndAwait(counterBean::increaseCounterWithHR); | ||
Counter counter = subscribeAndAwait(counterBean::findCounter); | ||
|
||
assertThat(counter.getCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void increaseCounterWithPanache() throws Throwable { | ||
subscribeAndAwait(counterBean::increaseCounterWithPanache); | ||
Counter counter = subscribeAndAwait(counterBean::findCounter); | ||
|
||
assertThat(counter.getCount()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void shouldReuseExistingSessions() throws Throwable { | ||
subscribeAndAwait(counterBean::assertThatSessionsAreEqual); | ||
} | ||
|
||
@Test | ||
void increaseCounter() throws Throwable { | ||
List<Supplier<Uni<Counter>>> suppliers = new ArrayList<>(); | ||
for (int i = 0; i < INCREASES_NUM; i++) { | ||
suppliers.add(() -> counterBean.increaseCounterWithHR()); | ||
} | ||
|
||
final List<Counter> results = new ArrayList<>(); | ||
suppliers.stream() | ||
.parallel() | ||
.forEach(uni -> increaseCounter(uni, results)); | ||
|
||
final Counter dbCounter = subscribeAndAwait(() -> counterBean.findCounter()); | ||
assertThat(dbCounter.getCount()).isEqualTo(INCREASES_NUM); | ||
} | ||
|
||
private static void increaseCounter(Supplier<Uni<Counter>> func, List<Counter> counters) { | ||
try { | ||
final var counter = subscribeAndAwait(func); | ||
counters.add(counter); | ||
} catch (final Throwable e) { | ||
fail(e); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of duplicating this method here, we could make it public in Hibernate Reactive and keep the
ContextualDataStorage.CONTEXTUAL_DATA_KEY
private.By doing so, the implementation details about how the
ConcurrentMap
is created/retrieved remains hidden, yet the consumers can manipulate the data it holds.