EJB3 Transaction Propagation

后端 未结 8 1697
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 04:25

I have a stateless bean something like:

@Stateless
public class MyStatelessBean implements MyStatelessLocal, MyStatelessRemote {
    @PersistenceContext(unit         


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 05:05

    Another way to do it is actually having both methods on the same bean - and having an @EJB reference to itself! Something like that:

    // supposing processObjects defined on MyStatelessRemote1 and process defined on MyStatelessLocal1
    @Stateless
    @TransationAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public class MyStatelessBean1 implements MyStatelessLocal1, MyStatelessRemote1 {
        @EJB
        private MyStatelessLocal1 myBean2;
    
        public void processObjects(List objs) {
            // this method just processes the data; no need for a transaction
            for(Object obj : objs) {
                this.myBean2.process(obj);
            }
        }
    
    
        @TransationAttribute(TransactionAttributeType.REQUIRES_NEW)
        public void process(Object obj) {
            // do some work with obj that must be in the scope of a transaction
    
            this.mgr.merge(obj);
            // ...
            this.mgr.merge(obj);
            // ...
            this.mgr.flush();
        }
    }
    
    
    

    This way you actually 'force' the process() method to be accessed via the ejb stack of proxies, therefore taking the @TransactionAttribute in effect - and still keeping only one class. Phew!

    提交回复
    热议问题