[英]EJB timer ignores TransactionTimeout
I have an EJB timer in an ear, deployed on JBoss 6.4. 我有个EJB计时器,部署在JBoss 6.4上。 In 99% of all cases the timer works fine, but sometimes it takes a bit too long to finish and throws a PersistenceException: Transaction was rolled back in a different thread!
在99%的所有情况下,计时器都可以正常工作,但是有时它可能需要很长时间才能完成,并抛出PersistenceException: Transaction was rolled back in a different thread!
. 。
I have found that this is due to a transacion timeout value that is too low. 我发现这是由于Transacion超时值太低所致。 I do not want to edit the default value but rather override the method-specific timeout. 我不想编辑默认值,而是要覆盖特定于方法的超时。 To solve this I split the functionality into two methods: one method annotated with @Timeout
and one which actually does the work. 为了解决这个问题,我将功能分为两种方法:一种是用@Timeout
注释的方法, @Timeout
一种是实际起作用的方法。
Here is my timer implememntation: 这是我的计时器实现:
@Singleton
public class MyTimer implements SomeTimerInterface {
@EJB
private SomeManager myManager;
@Resource
private TimerService timerService;
private Timer timer;
@Override
public void startTimer() {
// Timer scheduled to fire every 7th minute
ScheduleExpression schedule = new ScheduleExpression();
schedule = schedule.hour("*").minute("*/7").second("00");
TimerConfig config = new TimerConfig();
config.setPersistent(false);
timer = timerService.createCalendarTimer(schedule, config);
}
@Timeout
@AccessTimeout(value = 20, unit = TimeUnit.MINUTES)
public void handleTimeout() {
workInNewThread();
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
@TransactionTimeout(unit = TimeUnit.SECONDS, value = 900)
public void workInNewThread() {
// This can take anything from 1 sec to 15 min.
List<Object> objects = myManager.getAllTheDatabaseObjects();
}
}
However, the timer seems to ignore the TransactionTimeout as it still times out after 5 minutes (default value). 但是,计时器似乎忽略了TransactionTimeout,因为它在5分钟(默认值)后仍然超时。 How can I override the default timeout to make sure that the timer finishes the job? 如何覆盖默认超时以确保计时器完成作业?
Not sure whether I get it correct. 不知道我是否正确。 From your code it look like you call workInNewThread() from your @Timeout method, but there is a method cleanup() with a Tx attribute. 从您的代码看来,您好像从@Timeout方法调用workInNewThread(),但是有一个带有Tx属性的方法cleanup()。
I guess that you call methods in the same bean from your @Timeout. 我猜您从@Timeout在同一个bean中调用方法。 But in this case the annotations, neither @TxAttribute nor TxTimeout, will take effect as the container does not control the internal method invocation. 但是在这种情况下,@ TxAttribute和TxTimeout注释都不会生效,因为容器无法控制内部方法的调用。 You need to use a different EJB or a self reference to let the controller do that work. 您需要使用其他EJB或自引用来让控制器执行此工作。 Other option is to annotate the timeout method directly. 另一种选择是直接注释超时方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.