4 Replies Latest reply on Dec 14, 2010 2:06 PM by brandx

    PersistenceContextType.EXTENDED and Transactions

    brandx

      JBoss 5.1

      JPA 1

       

      The EXTENDED pattern of coding appears to be the model that fits my needs, but I'm having problems finding reference material for it that makes sense and works. As I understand it, every command I make which changes data gets queued up and doesn't commit until I call em.flush(). I've compiled and run like that, but I'm fairly sure that this method has no rollback functionality, which makes it useless for a production environment. All of the reference material that I can find says that you need to call em.getTransaction().begin() to start a transaction, but I get "A JTA EntityManager cannot use getTransaction()" as an error when I try to do that. But where I create my EntityManager like:

       

       

      @PersistenceContext(unitName="MySQL", type=PersistenceContextType.EXTENDED)
      private EntityManager em;

      @PersistenceContext(unitName="MySQL", type=PersistenceContextType.EXTENDED)

      private EntityManager em;

       

       

      The reference material gets it from an EntityManagerFactory. But, it never says where one can get an EntityManagerFactory from. I can inject one via @PersistenceUnit, but there's no option for EXTENDED there.

       

      My book says that I should set the session bean to @TransactionAttribute(value=TransactionAttributeType.NOT_SUPPORTED) and on a method by method basis set TransactionAttributeType.REQUIRED, but this results in no data being written to the database. With that, I can call em.getTransaction.begin()/commit() without error, but still no data is written to the database. Adding em.flush() doesn't change anything.

        • 1. Re: PersistenceContextType.EXTENDED and Transactions
          wdfink

          As i understand the EXTENDED persistence context ...

          The persistence manager is under user/programmer control, in this case it makes no sense to use it with a stateless bean it will break the contract.

          You might use it together with a stateful bean to have all data across multiple call (e.g. web formular with many pages) and commit only if the last access is successful. Transaction can also used, but you should limit the Tx scope because of timeouts and locks.

          • 2. Re: PersistenceContextType.EXTENDED and Transactions
            brandx

            I am using it in a stateful bean.

             

            But what exactly is the syntax for starting and stopping a transaction? All of the methods that I've been able to discern either result in an error or in no data being written to the database.

            • 3. Re: PersistenceContextType.EXTENDED and Transactions
              brandx

              Hm, well I found http://bill.burkecentral.com/category/jpa/, which says that I can use EXTENDED on an org.hibernate.Session and then use that instead of an EntityManager. I tried that, and was thence able to call Session's beginTransaction() and org.hibernate.Transaction's commit() method without error, and data was persisted to the database. Presuming that it's actually doing what it says, I guess that's my solution for the time being. I would have preferred to have stayed in standard JPA though, so if anyone knows how to do what I was trying to do in JPA, please let me know.

              • 4. Re: PersistenceContextType.EXTENDED and Transactions
                brandx

                Heh, and I just found yet another way to do it.

                 

                @Stateful

                @TransactionManagement(value=TransactionManagementType.BEAN)

                public class X implements IX {

                @PersistenceContext(unitName="MySQL", type=PersistenceContextType.EXTENDED)
                private EntityManager em;
                @Resource
                private UserTransaction tx;

                @PersistenceContext(unitName="MySQL", type=PersistenceContextType.EXTENDED)

                private EntityManager em;

                @Resource

                private UserTransaction tx;

                 

                public void doStuff() {

                tx.begin();

                em.joinTransaction();

                 

                ...

                 

                tx.commit();

                }

                }

                 

                Which is JPA compliant and does seem to work.