2 Replies Latest reply on Apr 15, 2010 3:42 PM by Donatas Ciuksys

    Injection and Persistence Context inheritance

    Donatas Ciuksys Newbie

      Hi CDI gurus,


      Persistence Context propagation makes living with EJBs easier, but CDI specification does not explicitly state whether (and how) propagation rules work when @Inject is being used instead of @EJB.


      In particular, my question is whether persistence context inheritance will take place, if one @SessionScoped stateful SB (with Extended Persistence Context) will @Inject another @SessionScoped stateful SB with Extended Persistence Context.


      For example: let A1, A2 and B all be stateful @SessionScoped session beans with extended persistence contexts each. Then:


      @Stateful
      @SessionScoped
      A1 {
        @Inject B b;
        ...
      }


      @Stateful
      @SessionScoped
      A2 {
        @Inject B b;
      }


      @Stateful
      @SessionScoped
      B {
      ...
      }


      Is persistence context inheritance happening?


      Cheers,
      Donatas

        • 1. Re: Injection and Persistence Context inheritance
          Pete Muir Master

          Donatas Ciuksys wrote on Apr 14, 2010 20:20:


          Hi CDI gurus,

          Persistence Context propagation makes living with EJBs easier, but CDI specification does not explicitly state whether (and how) propagation rules work when @Inject is being used instead of @EJB.

          In particular, my question is whether persistence context inheritance will take place, if one @SessionScoped stateful SB (with Extended Persistence Context) will @Inject another @SessionScoped stateful SB with Extended Persistence Context.

          For example: let A1, A2 and B all be stateful @SessionScoped session beans with extended persistence contexts each. Then:

          @Stateful
          @SessionScoped
          A1 {
            @Inject B b;
            ...
          }



          @Stateful
          @SessionScoped
          A2 {
            @Inject B b;
          }



          @Stateful
          @SessionScoped
          B {
          ...
          }



          Is persistence context inheritance happening?

          Cheers,
          Donatas


          Persistence context propagation should happen as though you had injected with @EJB IIRC. If this isn't the case with Weld, please raise an issue in JIRA so we can review.

          • 2. Re: Injection and Persistence Context inheritance
            Donatas Ciuksys Newbie

            Pete Muir wrote on Apr 15, 2010 13:03:


            Persistence context propagation should happen as though you had injected with @EJB IIRC. If this isn't the case with Weld, please raise an issue in JIRA so we can review.


            Well I believe my example above should create some problems for Weld implementers, if they would implement persistence context inheritance as it is specified by JPA specification. Citing (page 295):



            If the component is a stateful session bean to which an extended persistence context has been
            bound and there is a different persistence context bound to the JTA transaction, an EJBException
            is thrown by the container.


            Please look at my example case above: all EJBs are SessionScoped, so there is only one instance created for each of them (for a single user). Thus both A1 and A2 will inject one and the same instance of B.

            My question is: what persistence context will be bound to B? A1's? A2's? Or B's own?

            If all @Inject annotations would be replaced by @EJB, then we would have two instances of B, each having persistence context bound to corresponding A1/A2 component (no problems what so ever).

            But here we have only one instance of B, so this should be suspicious at least, if not erroneous.