EJB3 Transaction Propagation

后端 未结 8 1670
隐瞒了意图╮
隐瞒了意图╮ 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:00

    I think the thing is each bean is wrapped in a proxy that controls the transactional behaviour. When you call from one bean to another, you're going via that bean's proxy and the transaction behaviour can be changed by the proxy.

    But when a bean calls a method on itself with a different transaction attribute, the call doesn't go via the proxy, so the behaviour doesn't change.

    0 讨论(0)
  • 2020-12-13 05:04

    I haven't tried it yet (I'm about to), but an alternative to injecting a self-reference via the @EJB annotation is the SessionContext.getBusinessObject() method. This would be another way to avoid the possibility of a circular reference blowing things up on you - although at least for stateless beans injection does seem to work.

    I'm working on a large system in which both techniques are employed (presumably by different developers), but I'm not sure which is the "correct" way to do it.

    0 讨论(0)
  • 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<Object> 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!

    0 讨论(0)
  • 2020-12-13 05:05

    I had these circular dependency issues which Kevin mentioned. However, the proposed annotation @IgnoreDependency is a jboss-specific annotation and there is no counterpart in e.g Glassfish.

    Since it does not work with default EJB reference, I felt a bit uncomfortable with this solution.

    Therefore, I gave bluecarbon's solution a chance, thus starting the inner transaction "by hand".

    Beside this, I see no solution but to implement the inner process() in another bean which is also ugly because we simply want to disturb our class model for such technical details.

    0 讨论(0)
  • 2020-12-13 05:14

    In case someone comes across this one day:

    to avoid circular dependencies (allowing self reference for example) in JBoss use the annotation 'IgnoreDependency' for example:

    @IgnoreDependency @EJB MySelf myselfRef;

    0 讨论(0)
  • 2020-12-13 05:17

    Matt, for what it's worth I've come to exactly the same conclusion as you.

    TransactionAttributeTypes are only taken into consideration when crossing Bean boundaries. When calling methods within the same bean TransactionAttributeTypes have no effect, no matter what Types are put on the methods.

    As far as I can see there is nothing in the EJB Persistence Spec that specifies what the behaviour should be under these circumstances.

    I've also experienced this in Jboss. I'll also give it a try in Glassfish and let you know the results.

    0 讨论(0)
提交回复
热议问题