4 Replies Latest reply on Jul 30, 2007 10:14 AM by Carlo de Wolf

    BMT does not work the expected way.

    sumanta bhowmik Newbie

      I am trying to persist data through a BMT. The code is as shown below:
      package com.titan.travelagent;

      import javax.ejb.Stateless;
      import javax.ejb.TransactionManagement;
      import javax.ejb.TransactionManagementType;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;
      import javax.transaction.UserTransaction;

      @Stateless
      @TransactionManagement(TransactionManagementType.BEAN)
      public class TravelAgentBean {

      private static EntityManagerFactory factory = Persistence
      .createEntityManagerFactory("titan");

      public void createCabin(Cabin cabin) {

      EntityManager manager = factory.createEntityManager();
      try {
      UserTransaction utx;
      try {
      utx = (UserTransaction) ((new InitialContext())
      .lookup("UserTransaction"));
      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      utx.begin();
      manager.persist(cabin);
      utx.commit();

      } catch (Exception e) {

      e.printStackTrace();
      } finally {
      manager.close();
      }
      }



      }



      If I do "createCabin" it does not throw an exception , but I do not find the data persisted into the database. If I change the private static EntityManagerFactory factory = Persistence
      .createEntityManagerFactory("titan");
      to
      @PersistenceContext(unitName="titan")
      private EntityManager manager;

      It starts working. But as far I understand @PersistenceContext(unitName="titan")
      private EntityManager manager; is supposed to be used for CMT.

      Could someone please help and clarify if any thing else is required.

        • 1. Re: BMT does not work the expected way.
          wayne baylor Apprentice

          From the EJB3 Persistence spec (section 5.3, page 116) it says to use:

          EntityManagerFactory emf =
          javax.persistence.Persistence.createEntityManagerFactory("Order");
          EntityManager em = emf.createEntityManager();

          when working in a Java SE environment.

          When working in an EE container use injection/JNDI lookup. For example:
          @Stateless
          @TransactionManagement(TransactionManagementType.BEAN)
          public class TravelAgentBean {
          
          @PersistenceUnit(unitName="titan")
          private static EntityManagerFactory factory;
          ...}
          


          ps. you can get a UserTransaction with: @Resource UserTransaction tx; if you want to avoid the jndi lookup.

          • 2. Re: BMT does not work the expected way.
            sumanta bhowmik Newbie

            Hi

            Thanks for the response.

            Please look at 5.1 , second last paragraph. It says
            "Both container-managed entity managers and application-managed entity managers and their persistence contexts are required to be supported in Java EE web containers and EJB containers. "

            Would not that imply that the EntityManagerFactory factory = Persistence
            .createEntityManagerFactory("titan"); should work as well?

            • 3. Re: BMT does not work the expected way.
              wayne baylor Apprentice

              "Technically" it just needs to support the EntityManager, not the method of retrieving it.

              • 4. Re: BMT does not work the expected way.
                Carlo de Wolf Master

                From the same paragraph:

                In less common use cases within Java EE environments, applications may need to access a persistence context that is ?stand-alone??i.e. not propagated along with the JTA transaction across the EntityManager references for the given persistence unit.

                Never tried it like you have, but committing your JTA transaction doesn't make much sense.
                Try em.joinTransaction() or em.getTransaction().commit().
                Lets see what happens.