Set transaction timeout for BMT in JBoss EAP

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 4
    • 5
    • 6
    • 7

Issue

How can I set the timeout for a bean-managed transaction (BMT) in an EJB method?

Resolution

Per-transaction timeout can be set using the JTA API as illustrated below.

    UserTransaction ut = ... // get user transaction
    try
    {
       ut.setTransactionTimeout(600); // 600 seconds/10 minutes; must be set prior to starting the transaction
       ut.begin();
       // DO SOMETHING WITH TRANSACTIONAL RESOURCES
    }
    catch (Exception e)
    {
       ut.setRollbackOnly(); // Force a rollback for this error
       throw e;
    }
    finally
    {
       if (ut.getStatus() == Status.STATUS_ACTIVE)
          ut.commit();
       else
          ut.rollback();
    }

References

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.