2 Replies Latest reply on Jun 29, 2011 11:01 AM by rotapa

    PersistenceContext propagation

    rotapa

      Dear All,

       

      I've just downloaded the JBoss AS 6, I set up a working datasource and I tried to use my "test" beans.

       

      @Stateless

      public class Service1Bean implements Service1 {

          @PersistenceContext

          private EntityManager em;

       

          @EJB

          private Service2 service2;

       

          @Override

          public void doStuff() {

          User u = new User();

          user.setName("Name");

          em.persist(u);

          service2.doStuff(u.getId());

          }

      }

       

      @Stateless

      public class Service2Bean implements Service2 {

          @PersistenceContext

          private EntityManager em;

       

          @Override

          public void doStuff(Long id) {

          User u = em.find(User.class, id);

          System.out.println("user for id " + id + " is: " + u);

          }

      }

       

      I get user for id 13345 is: null

      the id is generated by a table generator, and the user gets saved to the db.

       

      but the persistence context is not propagated. As I said, I configured only the datasource, nothing else.

       

      Do I need to configure something to force the propagation?

       

      Thanks

        • 1. Re: PersistenceContext propagation
          wdfink

          I'm not sure whether the EM works without transaction context.

          Add

          @TransactionManagement(TMType.CONTAINER)

          @TransactionAttribute(REQUIRED)

          to your class definition and check if it works.

           

          Also it might happen that the two beans run in a different transaction context.

          Than your code might work if you call Service2 from the client, also the User should be in the database after the test is executed.

          • 2. Re: PersistenceContext propagation
            rotapa

            I think I got it.

             

            @TransactionManagement(TMType.CONTAINER) and @TransactionAttribute(REQUIRED) are the default so it shouldnt be a problem.

             

            I didnt write the whole class for simplicity. I have a stateful bean, with extended pc. I wanted to have an exception because of the pc collision.

            but it seems quite strange with jboss:

             

            so I have a stateful bean:

             

            @Stateful

            public class Service3Bean implements Service3 {

                @PersistenceContext(type=...EXTENDED)

                private EntityManager em;

             

                @Override

                public void doStuff(Long id) {

                User u = em.find(User.class, id);

                System.out.println("user for id " + id + " is: " + u);

                }

            }

             

            so service3 is the stateful, service2 is the stateless

             

            and in the service1's doStuff method:

             

                @Override

                public void doStuff() {

                User u = new User();

                user.setName("Name");

                em.persist(u);

                service3.doStuff(u.getId());

                service2.doStuff(u.getId());

                }

             

            in that case I get:

             

            user for id 13345 is: null

            user for id 13345 is: null

             

            If I change the order of invocations:

                @Override

                public void doStuff() {

                User u = new User();

                user.setName("Name");

                em.persist(u);

                service2.doStuff(u.getId());

                service3.doStuff(u.getId());

                }

             

            user for id 13345 is: pkg.User@aaaaaa

            user for id 13345 is: pkg.User@bbbbbb

             

            so if we call stateless before the stateful the pc is propagated, and it work for the extended pc as well.

            if we call the stateful before the stateless the pc doesnt get propagated.

             

            another interesting:

            if I call the stateless before stateful, and I modify the username in the stateless, (the pc is propagated to the stateless so we can do),

            and from the service1Bean we call the stateful the pc doesnt get propagated there, so the user there is null.

             

            It's quite extreme example, but as the hibernate reference jpa spec  say, an exception should be thrown if we try to call a stateful (with REQUIRED trancaction attribute) from stateless.

             

            It seems to be a bug/strange feature

             

            btw, Thank you for the fast reply