이 콘텐츠는 선택한 언어로 제공되지 않습니다.

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 @Transactional annotation on the entry method:

    Example src/main/java/org/acme/SantaClauseService.java

    import javax.inject.Inject;
    import javax.enterprise.context.ApplicationScoped;
    import javax.transaction.Transactional;
    
    @ApplicationScoped
    public class SantaClausService {
    
        @Inject ChildDAO childDAO;
        @Inject SantaClausDAO santaDAO;
    
        @Transactional 
    1
    
        public void getAGiftFromSanta(Child child, String giftDescription) {
            // some transaction work
            Gift gift = childDAO.addToGiftList(child, giftDescription);
            if (gift == null) {
                throw new OMGGiftNotRecognizedException(); 
    2
    
            }
            else {
                santaDAO.addToSantaTodoList(gift);
            }
        }
    }
    Copy to Clipboard Toggle word wrap

    1
    @Transactional annotation defines your transaction boundaries and wraps this call within a transaction.
    2
    When a RuntimeException crosses the transaction boundaries, the transaction manager rolls back the transaction.

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

    import javax.inject.Inject;
    import javax.enterprise.context.ApplicationScoped;
    import javax.transaction.Transactional;
    
    @ApplicationScoped
    public class SantaClausService {
    
        @Inject ChildDAO childDAO;
        @Inject SantaClausDAO santaDAO;
    
        @Transactional(dontRollbackOn=NonCriticalRuntimeException.class)
        public void getAGiftFromSanta(Child child, String giftDescription) throws Exception {
            Gift gift = childDAO.addToGiftList(child);
    
            // might throw a NonCriticalRuntimeException
            gift.setDescription(giftDescription);
    
            santaDAO.addToSantaTodoList(gift);
        }
    }
    Copy to Clipboard Toggle word wrap

    In this example, the transaction context is propagated to all calls nested in the @Transactional method (childDAO.addToGiftList() and santaDAO.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 timeout property of the @TransactionConfiguration to set the timeout in seconds:

    import javax.transaction.Transactional;
    
    @Transactional
    @TransactionConfiguration(timeout=40)
    public void getAGiftFromSanta(Child child, String giftDescription) {...}
    Copy to Clipboard Toggle word wrap
Note

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.

Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동