我有一个 J2EE 应用程序,其 bean 具有容器管理的 EntityManager。在长时间运行的方法调用中,尝试合并数据会引发
RollbackException (Timed out)
我曾尝试使用 EntityManagerFactory,但似乎不允许:
Cannot use an EntityTransaction while using JTA
如何在不设置不合理超时的情况下运行任意长的进程? JTA 不能在需要时创建新事务吗?
最佳答案
根据对我的问题的评论,此 question和文档 here ,我使用容器管理的 EntityManager 和 TransactionAttributeType
解决了这个问题。注释。
导致超时的 bean 方法现在对处理子任务的不同方法进行多次调用,例如每个方法调用在不同的事务中执行。我用 NOT_SUPPORTED
属性类型,因为:
If the client is not associated with a transaction, the container does not start a new transaction before running the method.
通过这种安排,只有较小的
processDoc
方法创建不应超时的事务。public class MyBean {
@EJB
private DocsBean docsBean;
/**
* This method transaction risks a timeout
* so no transaction is created with NOT_SUPPORTED
*/
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void longRunning(List<Document> docs) {
for (Document doc : docs) {
// a different transaction is used for each element
docsBean.processDoc(doc);
}
}
}
public class DocsBean {
/** Runs within a new transaction */
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void processDoc(Document document) {
// takes some time but under the timeout
// ...
}
}
.
关于jakarta-ee - 避免容器管理的 EntityManager 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26341801/