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

Chapter 5. Managing JTA transactions programmatically using the API approach


You can manage transaction boundaries programmatically by injecting UserTransaction. The following chapters demonstrate how you can manage JTA transactions and define transaction boundaries using the API approach.

5.1. Defining transaction boundaries using the API approach

You can inject a UserTransaction and manage the transaction boundaries by calling its begin(), commit() and rollback() methods.

Procedure

  1. Inject the UserTransaction interface:

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

    @ApplicationScoped
    public class SantaClausService {
    
        @Inject ChildDAO childDAO;
        @Inject SantaClausDAO santaDAO;
        @Inject UserTransaction transaction;
    }
    Copy to Clipboard Toggle word wrap

  2. Use the transaction demarcation methods to control the transaction:

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

    import javax.transaction.Transactional;
    import javax.inject.Inject;
    import javax.transaction.SystemException;
    import javax.transaction.UserTransaction;
    
    @ApplicationScoped
    public class SantaClausService {
    
        @Inject ChildDAO childDAO;
        @Inject SantaClausDAO santaDAO;
        @Inject UserTransaction transaction;
    
        public void getAGiftFromSanta(Child child, String giftDescription) {
            // some transaction work
            try {
                transaction.begin(); 
    1
    
                Gift gift = childDAO.addToGiftList(child, giftDescription);
                santaDAO.addToSantaTodoList(gift);
                transaction.commit();
            }
            catch(SomeException e) {
                // do something on Tx failure
                transaction.rollback(); 
    2
    
            }
        }
    }
    Copy to Clipboard Toggle word wrap

    1
    Place your transaction code between the transaction.begin() and the transaction.commit().
    2
    Aborts the transaction immediately.
    Note

    You cannot use UserTransaction in a method where a transaction starts by a @Transactional call.

5.2. Configuring a transaction for rollback using the API approach

Exceptions caused by system-level faults mark the transactions for rollback. You can mark the transaction for rollback programmatically by injecting TransactionManager.

Procedure

  1. Inject the TransactionManager and set the transaction for rollback with setRollbackOnly:

    In this example, the transaction context is propagated to all calls nested in the @Transactional method (childDAO.addToGiftList() and santaDAO.addToSantaTodoList()). The transaction manager commits the transaction unless a runtime exception crosses the method boundary.

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

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

    1
    Inject the TransactionManager to be able to activate setRollbackOnly semantic.
    2
    Programmatically decide when to roll back the transaction.
맨 위로 이동
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

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

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

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

Red Hat 소개

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

Theme

© 2026 Red Hat