javatransactionsejbstateless-session-beanstateful-session-bean

EJB Bean is associated with a different unfinished transaction


Please look at this snippet:

@Stateless 
public class A {
   @EJB
   B b; // B is stateful EJB

   b.init(); // initialize Sets and List that are used in doSthInB(C c)
   public void doSthInA(){
        for(C c){
           b.doSthInB(c); // doSthInB use REQUIRES_NEW transaction type
        }
   }
}

My intention is to process a collection of objects of type C. The method doSthInB modifies entities and the result should be persisted at the end of the method call (that is why I user REQUIRES_NEW). When during processing of some element from the input collection some error occurs then only this operation should not be persisted, it should be logged and the processing of other elements should not be stopped.

What I get during processing is:javax.ejb.EJBException: java.lang.IllegalStateException: Bean is associated with a different unfinished transaction

Maybe my idea is wrong. Any tips?


Solution

  • I see that you are injecting a Stateful in a Stateless bean. Despite the fact that this is allowed by the specification in the most cases this is an error.

    I don't know if you are completely aware of what this design decision implies. If not try to avoid to use this approach. I think that this probably be the cause of your exception.