Thursday, July 30, 2015

Arjuna016051 Thread Is Already Associated With A Transaction

This issue occurs when someone tries to begin a transaction in bean managed transaction process and previous tx is still in process or hung without either committing or rolling back.
I have experienced this issue while working on a transaction based web service application. In order to resolve this, I would recommend to check the status of the transaction object created. Add below lines of code before beginning a transaction, in order to resolve this issue.

UserTransaction transaction = ejbClient.createTX();
int txStatus = transaction.getStatus();
if (txStatus == Status.STATUS_MARKED_ROLLBACK || txStatus == Status.STATUS_ROLLEDBACK) {
         transaction.rollback();
    }

transaction.begin();

This way I have completely resolved the issue and able to continue with starting a new transaction. Please post your comments if there is any better way.