[英]How to solve transaction timeout due to heavy db call inside a loop - EJB
如主題所述,總超時時間為 120 秒,在處理事務時超過。 實現就像下面的偽代碼。
最初的想法是使用 TransactionAttributeType.NOT_SUPPORTED 將 methodA 從 tx 上下文中取出,並將代價高昂的 db 方法調用移動到 REQUIRES_NEW(新事務)。 但是考慮到方法 A 的實現,它非常忙碌,並且 db proc 已經優化到了最佳能力。
是否有任何解決方法:) 真的很感激
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;
}
更新:更改超時持續時間不是一個選項
對於Bean Managed Transactions,可以使用UserTransaction接口的setTransactionTimeout方法設置超時時間,對於Container Managed Transactions,應該使用服務器特定的配置(即對於JBOSS,可以使用注解org.jboss.ejb3.annotation.TransactionTimeout )。
例子:
@TransactionTimeout(unit=TimeUnit.MINUTES, value=10)
public void doAction() {
doAction 方法事務的有效期最長為 10 分鍾,同樣可以在 bean 管理的事務上使用以下方法處理:
public void doAction() {
try {
ut.setTransactionTimeout(60 * 10);
ut.begin();
請注意,該方法將參數作為秒(即 10 分鍾 = 10 * 60)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.