Cannot begin transacton in code called from @Asynchronous EJB method

Solution Verified - Updated -

Issue

  • When we try to start a transaction from code called by an @Asynchronous EJB method, it fails with
javax.transaction.NotSupportedException: BaseTransaction.checkTransactionState - ARJUNA016051: thread is already associated with a transaction!
  at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.begin(BaseTransaction.java:72)
  at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.begin(BaseTransactionManagerDelegate.java:65)
  at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.begin(ServerVMClientUserTransaction.java:142)
  at test.TestServlet$1.run(TestServlet.java:54)
  at test.MyEJB.run(TestServletBean.java:13)
class MyEJB {
  @Asynchronous public void run(Runnable r) {
    r.run();
  }
}


public class TestServlet extends HttpServlet {
  @EJB MyEJB ejb;

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ejb.run(new Runnable() {
      public void run() {
        try {
          InitialContext context = new InitialContext();
          UserTransaction transaction = (UserTransaction) context.lookup("java:jboss/UserTransaction");
          transaction.begin();
          transaction.commit();
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
  }

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 6.x

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.