Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.

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;
    }

  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
    
            }
        }
    }

    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);
            }
        }
    }

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

Lernen

Testen, kaufen und verkaufen

Communitys

Über Red Hat Dokumentation

Wir helfen Red Hat Benutzern, mit unseren Produkten und Diensten innovativ zu sein und ihre Ziele zu erreichen – mit Inhalten, denen sie vertrauen können. Entdecken Sie unsere neuesten Updates.

Mehr Inklusion in Open Source

Red Hat hat sich verpflichtet, problematische Sprache in unserem Code, unserer Dokumentation und unseren Web-Eigenschaften zu ersetzen. Weitere Einzelheiten finden Sie in Red Hat Blog.

Über Red Hat

Wir liefern gehärtete Lösungen, die es Unternehmen leichter machen, plattform- und umgebungsübergreifend zu arbeiten, vom zentralen Rechenzentrum bis zum Netzwerkrand.

Theme

© 2026 Red Hat
Nach oben