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.

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.

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

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust. Explore our recent updates.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

Theme

© 2026 Red Hat
Back to top