Tapas Bose
Tapas Bose

Reputation: 29806

EJB 3 Transaction Boundaries: Calling Non Transactional Class

I have an EJB 3 bean. In which the transaction management is container managed and the transactional attribute is required.

From one method of this bean I am instantiating another class, which is non-transactional and calling a method to delegate the persist task. In this method I am passing EntityManager instance, which is injected by the PersistanceContext in the above bean. Now when I am calling the EntityManage#persist I am getting exception saying:

The operation needs to be executed within a transaction

I am suspecting the cause of this exception is due to the Transaction is not available in this second class. Am I right?

If I don't want to change the code flow, is there alternate any way to achieve this?

Upvotes: 0

Views: 167

Answers (1)

damat-perdigannat
damat-perdigannat

Reputation: 5950

Yes, you're right. persist() needs to be called within a transaction. In some way, you have to remove the call to persist. Else, you might be forced to transform some of the non-transactional classes into Enterprise JavaBeans themselves.

Upvotes: 0

Related Questions