1 2 Previous Next 16 Replies Latest reply on Apr 1, 2008 7:10 AM by barbacena

    How to control transation cross multiple methods?

    gus888

      Hi there,


      I don't know how to control transaction cross two or more methods in Seam conversation session bean, shown below. Anybody can give a help? Thank you very much in advance.

      //EJB3 Container transaction
      
      private A a;
      
      @Begin
      public void createA() {
           a = new A()
           em.persist(a);
           ...
      }
      
      @End
      public void done() {
           ...
           //commit here;
      }
      
      @End
      public void cancel() {
           //rollback
      }

        • 1. Re: How to control transation cross multiple methods?
          gus888

          Anybody can give a little help, even a clue? Should the SFSB implements SessionSynchronization or it need Seam transaction? Thanks.

          • 2. Re: How to control transation cross multiple methods?
            gus888

            From Seam document, it seems that the codes should use flushMode=MANUAL, like:

            private A a;
            
            @Begin(flushMode=MANUAL)
            public void createA() {
                 a = new A()
                 em.persist(a);
                 ...
            }
            
            @End
            public void done() {
                 ...
                 em.flush();
            }
            
            @End
            public void cancel() {
                 //rollback ???
            }

            but, how to roll back when using Seam em? Thanks.

            • 3. Re: How to control transation cross multiple methods?
              jazir1979

              Hi Sheng,


              Transactions and flush mode are different things.  The createA() done() and cancel() methods in your example still all define a single transaction each, but with manual flush mode this doesn't matter because the SQL has not been sent to the DB until you manually call em.flush() in the done() method.


              Therefore the code in your example is probably already sufficient.  There is no need to rollback in the cancel() method, because you won't call em.flush() -- there is nothing to roll back.


              I hope that helps,


              Daniel.

              • 4. Re: How to control transation cross multiple methods?
                admin.admin.email.tld

                This may be a situation that requires BMT (bean managed transactions).  see pg. 328 of the JSR220-core spec (section 13.3.3).


                Alternatively you can do as you showed above and in the cancel() method, clear the EntityManager instance by calling em.clear().  This will cause all the managed entities in the persistence context to become detached.


                Acutally I think the @Destroy annotated method gets called after the @End annotated method is executed.  So you may not need to rollback since the EntityManager won't be flushed automatically in this case.  The persistence context goes out of scope (set to null) when the SFSB is destroyed I think.

                • 5. Re: How to control transation cross multiple methods?
                  gus888

                  Daniel Young wrote on Mar 28, 2008 01:11 AM:


                  Transactions and flush mode are different things.  The createA() done() and cancel() methods in your example still all define a single transaction each, but with manual flush mode this doesn't matter because the SQL has not been sent to the DB until you manually call em.flush() in the done() method.

                  Therefore the code in your example is probably already sufficient.  There is no need to rollback in the cancel() method, because you won't call em.flush() -- there is nothing to roll back.

                  Hi Daniel,


                  Thank you very much for your help. I tried my code. However, when I clicked cancel(), the value a persisted in createA() cannot be rolled back.

                  • 6. Re: How to control transation cross multiple methods?
                    gus888

                    Hi Arbi, sorry, I could not get your idea. Currently, I use CMT and Seam injected entity manager. I am not clear whether these two can work together. Anybody can give more help? Thanks a lot.

                    • 7. Re: How to control transation cross multiple methods?
                      pmuir

                      In your code, the persisted entity shouldn't actually be flushed to the database until done() is called - so, as long as you don't call flush() in cancel() it should work. In fact, manual flush mode was designed to do exactly what you request - an atomic conversation. So, there is no need to do a rollback.


                      I'm not sure that BMT would help here.

                      • 8. Re: How to control transation cross multiple methods?
                        gus888

                        Hi Pete,


                        Thank you very much for your reply, especially in the weekend. I don't know what is wrong in my project. In my pages.xhtml, the code snippets of conversation:

                        <begin-conversation join="true" flush-mode="MANUAL"/>

                        . I also tried to add the following codes in persistence.xml:
                        <property name="hibernate.transaction.flush_before_completion" value="true"/>

                        but it also didn't work. When I click create(), I noticed server console displayed insert into A ..... When I click cancel(), there was nothing about delete from ... displayed on server console. Is my injected entityManager wrong:
                             @In(value = "entityManager", create=true, required=false)
                             protected EntityManager em;

                        I really appreciate your help!

                        • 9. Re: How to control transation cross multiple methods?
                          pmuir

                          The problem is not that there is no delete, but that there is an insert in the first place. This implies to me that the manual flush mode is not taking effect. Make sure that you start the conversation with manual flush mode in the request before doing the persist (this is an oddity in Seam that we should really iron out).

                          • 10. Re: How to control transation cross multiple methods?
                            barbacena

                            Did you try @Begin(flushMode = FlushModeType.MANUAL) ?

                            • 11. Re: How to control transation cross multiple methods?
                              gus888

                              Marcell Barbacena wrote on Mar 29, 2008 09:43 PM:


                              Did you try @Begin(flushMode = FlushModeType.MANUAL) ?

                              Hi Marcell, yes, I also tried this, but it also didn't work. As long as I clicked create() button, a new conversation started, and insert into A ... appeared in sql console.

                              • 12. Re: How to control transation cross multiple methods?
                                barbacena

                                Post your persistent.xml, components.xml and default configurations at pages.xml.


                                Because Seam use more then one transaction in a JSF life cycle, it may be a parameter thing.


                                ps.: if you use hibernate post the configs as well.

                                • 13. Re: How to control transation cross multiple methods?
                                  gus888

                                  Hi Marcell,


                                  My persistence.xml:

                                  <persistence-unit name="myPro">
                                       <provider>org.hibernate.ejb.HibernatePersistence</provider>
                                       <jta-data-source>java:/MyProDS</jta-data-source>
                                       <properties>
                                            <property name="hibernate.hbm2ddl.auto" value="none"/>
                                            <property name="hibernate.show_sql" value="true"/>
                                  
                                            <property name="hibernate.cache.provider_class" 
                                                      value="org.hibernate.cache.HashtableCacheProvider"/>
                                            <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                                  
                                            <property name="jboss.entity.manager.factory.jndi.name" value="java:/myProEntityManagerFactory"/>
                                       </properties>
                                  </persistence-unit>


                                  components.xml:
                                  <persistence:managed-persistence-context name="entityManager"
                                            auto-create="true"
                                            persistence-unit-jndi-name="java:/myProEntityManagerFactory"/>


                                  pages.xml:
                                  <rule if-outcome="editCourse">
                                       <begin-conversation join="true" flush-mode="MANUAL"/>
                                       <redirect view-id="/myPro/edit_course.xhtml"/>
                                  </rule>

                                  Thank you in advance.

                                  • 14. Re: How to control transation cross multiple methods?
                                    barbacena

                                    Comparing your config with mine, it is just hibernate.transaction.manager_lookup_class that is different. I am not using it.
                                    However, i don't believe that is the problem. Anyway give it try removing it.


                                    ps.: what is your hibernate, seam and jboss version?

                                    1 2 Previous Next