英文:
How to solve transaction timeout due to heavy db call inside a loop - EJB
问题
如题所述,总超时时间为120秒,在处理事务时已经超过了这个时间。实现如下伪代码。
最初的想法是使用TransactionAttributeType.NOT_SUPPORTED将methodA从tx上下文中拿出来,将昂贵的数据库方法调用移动到REQUIRES_NEW(新事务)中。但考虑到方法A的实现方式,这样做相当繁琐。而且数据库过程已经被优化到了最佳状态。
这个问题是否有解决方法呢 非常感谢!
public String methodA(x){
String resp;
------ 其他实现
if(x==10){
for(int k; k < 10 ; k++){
dbcall() --- 一个耗时约为15秒的重型数据库过程
}
}
------ 其他实现
return resp;
}
更新:更改超时持续时间不是一个选项。
英文:
As described in the topic the total timeout is 120s, it exceeds when processing the transaction. 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). But it's quite hectic considering the implmentation of method A. and the db proc is already optimized to a best of ability.
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
答案1
得分: 1
对于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秒)。
英文:
> 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 (i.e. for JBOSS, you can use the
> annotation 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:
public void doAction() {
try {
ut.setTransactionTimeout(60 * 10);
ut.begin();
Note that the method takes the parameters as seconds (i.e. 10 minutes = 10 * 60).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论