-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat: read only sessions #10077
Closed
Closed
feat: read only sessions #10077
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
...ernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/ReadOnlyTransactionTest.java
This file contains 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,94 @@ | ||
package io.quarkus.hibernate.orm; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.TypedQuery; | ||
import javax.transaction.Transactional; | ||
|
||
import org.hibernate.FlushMode; | ||
import org.hibernate.Session; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.hibernate.orm.enhancer.Address; | ||
import io.quarkus.hibernate.orm.runtime.SessionConfiguration; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ReadOnlyTransactionTest { | ||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClass(Address.class) | ||
.addAsResource("application.properties")); | ||
|
||
@Inject | ||
EntityManager entityManager; | ||
|
||
@BeforeEach | ||
@Transactional | ||
void init() { | ||
Address adr = new Address(); | ||
adr.setStreet("rue de Paris"); | ||
entityManager.persist(adr); | ||
entityManager.flush(); | ||
} | ||
|
||
@AfterEach | ||
@Transactional | ||
void destroy() { | ||
int deleted = entityManager.createQuery("delete from Address where street = 'rue de Paris'").executeUpdate(); | ||
assertEquals(1, deleted); | ||
entityManager.flush(); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
@SessionConfiguration(readOnly = true) | ||
public void testRO() { | ||
TypedQuery<Address> query = entityManager.createQuery("from Address where street = 'rue de Paris'", Address.class); | ||
Address result = query.getSingleResult(); | ||
assertNotNull(result); | ||
|
||
Session session = entityManager.unwrap(Session.class); | ||
assertTrue(session.isDefaultReadOnly()); | ||
assertEquals(FlushMode.MANUAL, session.getHibernateFlushMode()); | ||
} | ||
|
||
@Test | ||
@Transactional(Transactional.TxType.REQUIRES_NEW) | ||
@SessionConfiguration(readOnly = true) | ||
public void testSubTransactions() { | ||
TypedQuery<Address> query = entityManager.createQuery("from Address where street = 'rue de Paris'", Address.class); | ||
Address result = query.getSingleResult(); | ||
assertNotNull(result); | ||
|
||
Session session = entityManager.unwrap(Session.class); | ||
assertTrue(session.isDefaultReadOnly()); | ||
assertEquals(FlushMode.MANUAL, session.getHibernateFlushMode()); | ||
|
||
// as it's a new transaction, it works | ||
newTransaction(); | ||
} | ||
|
||
@Transactional(Transactional.TxType.REQUIRES_NEW) | ||
@SessionConfiguration(readOnly = false) | ||
public void newTransaction() { | ||
Session session = entityManager.unwrap(Session.class); | ||
assertFalse(session.isDefaultReadOnly()); | ||
assertEquals(FlushMode.AUTO, session.getHibernateFlushMode()); | ||
|
||
Address adr = new Address(); | ||
adr.setStreet("rue du paradis"); | ||
entityManager.persist(adr); | ||
entityManager.flush(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ns/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/SessionConfiguration.java
This file contains 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,26 @@ | ||
package io.quarkus.hibernate.orm; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import io.quarkus.narayana.jta.runtime.AdditionalTransactionConfiguration; | ||
|
||
/** | ||
* This annotation can be used to configure the Hibernate session. | ||
*/ | ||
@Inherited | ||
@Target({ ElementType.METHOD, ElementType.TYPE }) | ||
@Retention(value = RetentionPolicy.RUNTIME) | ||
@AdditionalTransactionConfiguration | ||
public @interface SessionConfiguration { | ||
/** | ||
* Whether or not the transaction performs read only operations on the underlying transactional resource. | ||
* Depending on the transactional resource, optimizations can be performed in case of read only transactions. | ||
* | ||
* @return true if read only. | ||
*/ | ||
boolean readOnly() default false; | ||
} |
This file contains 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
16 changes: 16 additions & 0 deletions
16
...ime/src/main/java/io/quarkus/narayana/jta/runtime/AdditionalTransactionConfiguration.java
This file contains 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,16 @@ | ||
package io.quarkus.narayana.jta.runtime; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This is a meta annotation that indicates that the child annotation defines additional transactional configuration. | ||
*/ | ||
@Inherited | ||
@Target({ ElementType.ANNOTATION_TYPE }) | ||
@Retention(value = RetentionPolicy.RUNTIME) | ||
public @interface AdditionalTransactionConfiguration { | ||
} |
This file contains 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.
I think this should be cached, as it is slow. Ideally we would pre-compute this at build time.
Also does this work in native mode? I think it will but an integration test would be helpful.