Search code examples
javahibernatejpaejb-3.0jboss6.x

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) taking way too much time for commit transaction


In our application we create new Transaction for our business logic.So for that we first mark NOT_SUPPORTED to our wrapper method and then that wrapper method calls actual business logic method which have REQUIRES_NEW on it.Now the problem is that when the call come back to wrapper method the diff of time is nearly 40% to 50% of whole API time. Here is the snippet of my code:

A.java

public Object A(){
    long stime = System.currentTimeMillis()
     b.BWrapper();  
     sysout("Time taken by API :"+System.currentTimeMillis() - stime);
}

B.java

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public Object BWrapper(){

     B();
     sysout("Time just after method B call:"+System.currentTimeMillis());
     return ob;
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public Object B(){

    sysout("Time before returning ob:"+System.currentTimeMillis());
    return ob;
} 

So,suppose if Time taken by API : 1 sec then the diff between Time before returning ob: and Time just after method B call: would be like 400 to 500 milliseconds which is almost 40% to 50% of total time. And there is no other logic in-between two sysout operation.

So what is the reason behind this?


Solution

  • There is logic behind the scenes that the EJB framework does for you, namely committing the transaction, which can be costly. It is done just after the return of the method.

    Understand, that since it is transactional changes inside the business method doesn't really do any database commits, so your sysout's may practically be correct, but the real heavy work is done just after that, at method return.

    public Object B() {
        //start tx
        doSomeDatabaseChange(); //quite fast
    
        return obj;
        // commit or rollback tx, may take time
    }