Avoid timeout in container-managed EntityManager

I have a J2EE application that beans has a container-managed EntityManager. On long method calls trying to combine data, throws

Rollback (excluded)

I tried using EntityManagerFactory, but it doesn't seem valid:

Cannot use EntityTransaction when using JTA

How can I start arbitrarily long processes without setting an unreasonable timeout? Can't create a new JTA transaction if necessary?

+4
source share
2 answers

, question , , EntityManager TransactionAttributeType.

bean, , , , , . NOT_SUPPORTED, :

, .

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
        // ...
    }
}

.

+4

UserTransaction?

@Resource
private UserTransaction tx;

@TransactionAttribute(TransactionAttributeType.NotSupported)
public void merge(SomeEntity e) {
   tx.setTransactionTimeout(60 * 1000); //one minute
   tx.begin();
   try {
      entityManager.merge(e);
      tx.commit();
   } catch(IllegalArgumentException ex) { 
       tx.rollback();
   }
}
+2

All Articles