이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 4. Managing JTA transactions declaratively using the annotations
You can let the container demarcate transaction boundaries by automatically beginning and committing JTA transactions based on annotations. The following chapters demonstrate how you can manage JTA transactions and define transaction boundaries using the @Transactional annotation.
4.1. Defining transaction boundaries declaratively 링크 복사링크가 클립보드에 복사되었습니다!
You can use @Transactional to control transaction boundaries on any CDI bean at the method level or at the class level to ensure that every method is transactional. This also applies to REST endpoints.
Procedure
Define the scope of the transaction with the
@Transactionalannotation on the entry method:Example src/main/java/org/acme/SantaClauseService.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2. Configuring a transaction for rollback declaratively 링크 복사링크가 클립보드에 복사되었습니다!
Exceptions caused by system-level faults mark the transactions for rollback and abort the transaction immediately. You can override the default behavior using the @Transactional(dontRollbackOn=SomeException.class) or the rollbackOn attribute.
Prerequisites
- Have a Quarkus Maven project.
Procedure
Use the
@Transactional(dontRollbackOn=SomeException.class)to specify an exception that does not roll back the transaction:Example src/main/java/org/acme/SantaClauseService.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this example, the transaction context is propagated to all calls nested in the
@Transactionalmethod (childDAO.addToGiftList()andsantaDAO.addToSantaTodoList()). The transaction commits unless a runtime exception crosses the method boundary.
4.3. Configuring a transaction timeout declaratively 링크 복사링크가 클립보드에 복사되었습니다!
Use the @TransactionConfiguration annotation in addition to the @Transactional annotation to specify the timeout in seconds. You can place the @TransactionConfiguration annotation only on the top-level method that delineates the transaction.
Procedure
Use the
timeoutproperty of the@TransactionConfigurationto set the timeout in seconds:import javax.transaction.Transactional; @Transactional @TransactionConfiguration(timeout=40) public void getAGiftFromSanta(Child child, String giftDescription) {...}import javax.transaction.Transactional; @Transactional @TransactionConfiguration(timeout=40) public void getAGiftFromSanta(Child child, String giftDescription) {...}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The configuration defined on a method takes precedence over the configuration defined on a class. When you define @TransactionConfiguration on a class, it is equivalent to defining it on all the methods of the class that are marked with @Transactional.
4.4. Methods returning reactive values 링크 복사링크가 클립보드에 복사되었습니다!
If a method annotated with @Transactional returns a reactive value it does not terminate the transaction until the returned reactive value is terminated. The transaction is marked for rollback when the reactive value terminates with an exception, otherwise the transaction is committed.