6 Replies Latest reply on Jun 11, 2010 5:10 AM by nathan dennis

    Transaction Management in Seam 2.2.0.GA

    nathan dennis Expert

      jboss seam 2.2.0.GA
      jboss AS 5.1.0.GA


      upgrading a project that have been running successfully for months on seam 2.1 i have ran in to a problem with Transaction Management. i can seem to find what changed from 2.1 to 2.2.0 and as many of you have realized this is by far the most confusing aspect of this glorious framework.


      as i said before this code had been running flawlessly .... (i'll truncate for simplicity)


      @Name("nightlyProcessingTMAction")
      @Stateless
      @TransactionManagement(TransactionManagementType.BEAN)
      public class NightlyProcessingTMAction implements NightlyProcessingTMLocal {
           
           @In
           private EntityManager entityManager;
           
           @Resource
           UserTransaction serTransaction;
           
           
           public Boolean nightlyProcess() {
                    try{
      //extend timeout          
                serTransaction.setTransactionTimeout(1000000);
                serTransaction.begin();
                      entityManager.joinTransaction();
      
                //purge the tpinventory table to tell if any have been deleted
                Query tppurge = (Query) entityManager.createNativeQuery("delete from Tpinventory");
                tppurge.executeUpdate();
                entityManager.flush();
                serTransaction.commit();
               } catch (Exception e1) {
                   // failed to set the transaction timeout => just log some content
                   e1.printStackTrace();
               }
                
                return true;
           }
           @TransactionAttribute(TransactionAttributeType.SUPPORTS)
           public void processIncoming(){
                //do a bunch of processing that if an error occurs we dont want the a huge roll back.. just the one transaction
                      // that we join and dispose of after each persist. as above...
      
              }
                
      
      


      this was a good solution that we worked very hard on less than a year ago.


      im pretty sure this has something to do with the changes to the transaction management in 2.2.0


      the error


      01:06:01,289 DEBUG [Component] trying to inject with hierarchical context search: entityManager
      01:06:01,294 SEVERE [application] java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:comp/env/crookv2/pu
      javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: EntityManagerFactory not found in JNDI : java:comp/env/crookv2/pu
              at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
              at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
              at javax.faces.component.UICommand.broadcast(UICommand.java:387)
              at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:324)
              at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:299)
              at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:256)
      
      


      components.xml


         
      <persistence:managed-persistence-context name="entityManager" auto-create="true"
                            persistence-unit-jndi-name="@puJndiName@"/>
      


      web.xml


         <persistence-unit-ref>
            <persistence-unit-ref-name>crookv2/pu</persistence-unit-ref-name>
            <persistence-unit-name>../crookv2.jar#crookv2</persistence-unit-name>
         </persistence-unit-ref>
      


      persistence.xml


         <persistence-unit name="crookv2">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>crookv2Datasource</jta-data-source>
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
               <property name="hibernate.hbm2ddl.auto" value="update"/>
                 <property name="hibernate.default_schema" value="public"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/crookv2EntityManagerFactory"/>
            </properties>
         </persistence-unit>
      


      whats happened too me here? as i said this method was working very well... and still is in production with 2.1.0 on a jboss 5 installation.
      ive been reading everything i can find trying to figure out what is going on.
      i found this article  DeployingToJboss5
      but that isnt quite the same thing.


      any help would be GREATLY appreciated.

        • 1. Re: Transaction Management in Seam 2.2.0.GA
          nathan dennis Expert

          this really should be in the migration guide as these sort of issues are its defining purpose.


          i would be exponentially helpful if one of the developers (or someone else that knows) would talk just a bit about what is actually happening here even though most of the greater minds are consumed by the WELD/Seam 3 development. just point us to some reference so other like me and I can figure this out. making it work is good, but concept is much more powerful.


             <persistence:managed-persistence-context name="entityManager" auto-create="true"
                                persistence-unit-jndi-name="java:/crookv2EntityManagerFactory"/>
          
          



          as the article said, remove the


           <persistence-unit-ref>
          
          


          makes it go back to working.

          • 2. Re: Transaction Management in Seam 2.2.0.GA
            Nikolay Elenkov Master

            Glad you found the cause. Do you really need to use the SMPC for patch processing? @PersitenceContext EntityManager makes more sense here and will give you less trouble.


            • 3. Re: Transaction Management in Seam 2.2.0.GA
              Nikolay Elenkov Master

              Nikolay Elenkov wrote on Jun 11, 2010 03:39:


              Glad you found the cause. Do you really need to use the SMPC for patch processing?


              Of course I meant batch processing.

              • 4. Re: Transaction Management in Seam 2.2.0.GA
                nathan dennis Expert

                or so it would seem.... obviously making use to REQUIRES.NEW on a method doing work, but we never could get that to work in the previous version. this on the other hand gave us full control of the Transaction, role back etc. really couldnt find many examples of this sort of thing integrated or using the seam framework. lots of examples of how to manage conversation context and serving up content, not much on true business processes  (!jbpm.. though it maybe included in that and i just dont know it) bulk data processing in the background without user interaction.


                what we couldnt have was a transaction that if record 10001 failed 1-10000 gets rolled back. sometimes the old fashion way is simplest.


                it still would be a great help to know what was happening with the above example/ what the seams developers were up to that caused it.

                • 5. Re: Transaction Management in Seam 2.2.0.GA
                  Nikolay Elenkov Master

                  Well, UserTransaction and REQUIRES_NEW are standard JEE, you don't need Seam to use those. Neither should their behaviour depend on Seam or its version. Seam doesn't really target background processing, maybe that's why there aren't many examples. The Quartz integration is quite useful for this kind of thing though.


                  As for the changes in 2.2, track the history of ManagedPersitenceContext in SVN and you should be able to follow what changed.


                  • 6. Re: Transaction Management in Seam 2.2.0.GA
                    nathan dennis Expert

                    the above method is called from Quartz. youre right on the examples. most of the JEE documentation (at least that i have found on the shelves) is outdated by the time it makes it to print. i need to find a good book or site on that i guess.


                    ill check out the SVN and see if i can figure out what they changed.