@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 花费太多时间提交事务

@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?

EJB 框架为您执行的幕后逻辑,即提交事务,这可能代价高昂。它是在方法的 return 之后完成的。

理解,因为它是业务方法内部的事务性更改,实际上并没有进行任何数据库提交,所以您的 sysout 实际上可能是正确的,但真正繁重的工作是在那之后完成的,在方法 return.

public Object B() {
    //start tx
    doSomeDatabaseChange(); //quite fast

    return obj;
    // commit or rollback tx, may take time
}