简体   繁体   English

如何解决由于循环内大量数据库调用而导致的事务超时 - EJB

[英]How to solve transaction timeout due to heavy db call inside a loop - EJB

As described in the topic the total timeout is 120s, it exceeds when processing the transaction.如主题所述,总超时时间为 120 秒,在处理事务时超过。 Implementations is like below psuedocode.实现就像下面的伪代码。

Original idea is to take the methodA out of tx context using TransactionAttributeType.NOT_SUPPORTED and move the costly db method call to a REQUIRES_NEW (New transaction).最初的想法是使用 TransactionAttributeType.NOT_SUPPORTED 将 methodA 从 tx 上下文中取出,并将代价高昂的 db 方法调用移动到 REQUIRES_NEW(新事务)。 But it's quite hectic considering the implmentation of method A. and the db proc is already optimized to a best of ability.但是考虑到方法 A 的实现,它非常忙碌,并且 db proc 已经优化到了最佳能力。

Is there any workaround for this :) Really appreciated是否有任何解决方法:) 真的很感激

public String methodA(x){
 String resp;
    ------ other implementations
    
    if(x==10){
    
        for(int k; k < 10 ; k++){
            dbcall() --- method with heavy db proc which costs around 15seconds
        }
    }
    ------ other implementations

   return resp;
}

UPDATE : changing the timeout duration is not an option更新:更改超时持续时间不是一个选项

For Bean Managed Transactions, you can use the method setTransactionTimeout of the UserTransaction interface to set the timeout, for Container Managed Transactions, server specific configurations should be used (ie for JBOSS, you can use the annotation org.jboss.ejb3.annotation.TransactionTimeout).对于Bean Managed Transactions,可以使用UserTransaction接口的setTransactionTimeout方法设置超时时间,对于Container Managed Transactions,应该使用服务器特定的配置(即对于JBOSS,可以使用注解org.jboss.ejb3.annotation.TransactionTimeout )。

Example:例子:

    @TransactionTimeout(unit=TimeUnit.MINUTES, value=10)
    public void doAction() {
    

The doAction method transaction will be valid for maximum 10 minutes, the same can be handled with the following on bean managed transactions: doAction 方法事务的有效期最长为 10 分钟,同样可以在 bean 管理的事务上使用以下方法处理:

    public void doAction() {
        try {
            ut.setTransactionTimeout(60 * 10);
            ut.begin();

Note that the method takes the parameters as seconds (ie 10 minutes = 10 * 60).请注意,该方法将参数作为秒(即 10 分钟 = 10 * 60)。

Resource 资源

Resource 2 资源2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM