-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
194 additions
and
0 deletions.
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
...nate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/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.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; | ||
|
||
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