7 Replies Latest reply on May 29, 2009 6:26 PM by Konstantin Konstantinopel

    Seam and Transactions: How can I rollback?

    Konstantin Konstantinopel Newbie

      Hi @ All!


      I want to realize a typical transaction like this: I've an conversation with three pages (a Wizard) where I make different changes on different objects. On the last page I want to commit all changes or discard all changes.


      I've tried it like this: On the first page I've started an conversation with flush mode set on MANUAL and on the last page, I've made an em.flush(). But at the first occurs of  em.merge() the object was written to the database although no em.flush() was called.


      Now my question: Why does it not work? What is the alternative to perform an rollback? I've read about the @Rollback annotation but it doesn't exist in my seam version (2.1.2 GA). If I call an method on em.getTransaction() I get always on error. It is true that it isn't allowed to use this object transaction object in Seam?


      If you need more informations to help me, please let it now me!


      Bye
      Cornelius


      PS: I inject the EntityManager like this:
          @PersistenceContext
          private EntityManager em;

        • 1. Re: Seam and Transactions: How can I rollback?
          Arbi Sookazian Master

          try this instead:


          @In private EntityManager em;



          what you were using is a transaction-scoped EJB container-managed EntityManager/PC.  You need to inject a SMPC.

          • 2. Re: Seam and Transactions: How can I rollback?
            Konstantin Konstantinopel Newbie

            Thank you for this hint. I've read the chapter 9.3.1. Using a Seam-managed persistence context with JPA from the Seam documentation again and I want to use now a Seam-managed persistence context. But I get always a ClassCastException. :-(


            This is the exception:


            Caused by: java.lang.ClassCastException: org.jboss.ejb3.entity.InjectedEntityManagerFactory cannot be cast to javax.persistence.EntityManagerFactory
            at org.jboss.seam.persistence.ManagedPersistenceContext.getEntityManagerFactoryFromJndiOrValueBinding(ManagedPersistenceContext.java:241)
            at org.jboss.seam.persistence.ManagedPersistenceContext.initEntityManager(ManagedPersistenceContext.java:78)
            



            What's the reason for this exception? I've found two posts with the same exception (1, 2) but it doesn't help me.


            My configurations:


            ---


            in WEB-INF/components.xml in .war:


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



            ---


            in META-INF/persistence.xml in .jar:


            <persistence-unit name="PortalExampleDatabase">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>  
                <jta-data-source>java:/PortalExampleDatasource</jta-data-source>  
                <properties> 
                  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>  
                  <property name="hibernate.hbm2ddl.auto" value="create-drop"/>  
                  <property name="hibernate.show_sql" value="true"/>  
                  <!-- These are the default for JBoss EJB3, but not for HEM: -->  
                  <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:/PortalExampleEntityManagerFactory"/> 
                </properties> 
              </persistence-unit>
            



            ---


            in Session Bean in .jar:


            @In
            protected EntityManager portalExampleEm;
            



            ---


            I hope, anyone can helps me!


            Bye
            Cornelius

            • 3. Re: Seam and Transactions: How can I rollback?
              Arbi Sookazian Master

              I have never seen that exception.


              How did you generate the code for components.xml and persistence-dev.xml?  Did you use seam-gen to reverse engineer entity classes and create your project-skeleton?


              Here is my code for comparison:


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



              <persistence-unit name="boBETS">
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>
                    <jta-data-source>java:/boBETSDatasource</jta-data-source>
                    <properties>
                       <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
                       <!-- <property name="hibernate.hbm2ddl.auto" value="validate"/>   -->
                       <property name="hibernate.show_sql" value="true"/>
                       <property name="hibernate.format_sql" value="true"/>
                       <property name="hibernate.generate_statistics" value="true"/>
                       <property name="jboss.entity.manager.factory.jndi.name" value="java:/boBETSEntityManagerFactory"/>
                       <property name="hibernate.default_catalog" value="boBETS"/>
                       <property name="hibernate.default_schema" value="dbo"/>
                    </properties>
                 </persistence-unit>

              • 4. Re: Seam and Transactions: How can I rollback?
                Konstantin Konstantinopel Newbie

                Arbi Sookazian wrote on May 21, 2009 21:38:


                How did you generate the code for components.xml and persistence-dev.xml?  Did you use seam-gen to reverse engineer entity classes and create your project-skeleton?



                I don't use seam-gen.



                I've put the jboss-ejb3.jar (found in $jboss/server/default/deploy/ejb3.deployer/) in my WEB-INF/lib folder. This jar file includes org.jboss.ejb3.entity.InjectedEntityManagerFactory. After this, the exception wasn't thrown. But an other one. Maybe a progress? Or a misleading way?


                The new exception:


                Caused by: org.jboss.seam.InstantiationException: Could not instantiate Seam component: alleLehrgaengeManager
                     at org.jboss.seam.Component.newInstance(Component.java:2106)
                     at org.jboss.seam.Component.getInstance(Component.java:1988)
                     at org.jboss.seam.Component.getInstance(Component.java:1950)
                     at org.jboss.seam.Component.getInstance(Component.java:1944)
                     at org.jboss.seam.Component.getInstanceFromFactory(Component.java:2036)
                     at org.jboss.seam.Component.getInstance(Component.java:1978)
                     at org.jboss.seam.Component.getInstance(Component.java:1950)
                     at org.jboss.seam.Component.getInstance(Component.java:1944)
                     at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:55)
                     at org.jboss.seam.Namespace.getComponentInstance(Namespace.java:50)
                     at org.jboss.seam.el.SeamELResolver.resolveBase(SeamELResolver.java:166)
                     at org.jboss.seam.el.SeamELResolver.getValue(SeamELResolver.java:53)
                     at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:53)
                     at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
                     at org.jboss.el.parser.AstIdentifier.getValue(AstIdentifier.java:44)
                     at org.jboss.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
                     at com.sun.facelets.el.TagValueExpression.getValue(TagValueExpression.java:71)
                     at javax.faces.component.UIData.getValue(UIData.java:609)
                     at org.ajax4jsf.component.UIDataAdaptor.getValue(UIDataAdaptor.java:1624)
                     at org.ajax4jsf.component.SequenceDataAdaptor.getDataModel(SequenceDataAdaptor.java:65)
                     at org.ajax4jsf.component.SequenceDataAdaptor.createDataModel(SequenceDataAdaptor.java:59)
                     at org.richfaces.component.UIDataTable.createDataModel(UIDataTable.java:120)
                     at org.ajax4jsf.component.UIDataAdaptor.getExtendedDataModel(UIDataAdaptor.java:621)
                     at org.ajax4jsf.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:339)
                     at org.richfaces.renderkit.AbstractTableRenderer.encodeTableStructure(AbstractTableRenderer.java:138)
                     at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:160)
                     at org.richfaces.renderkit.html.DataTableRenderer.doEncodeBegin(DataTableRenderer.java:148)
                     at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:100)
                     at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
                     at javax.faces.component.UIData.encodeBegin(UIData.java:962)
                     at org.ajax4jsf.component.UIDataAdaptor.encodeBegin(UIDataAdaptor.java:1202)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:934)
                     at javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
                     at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
                     at javax.faces.component.UIComponent.encodeAll(UIComponent.java:942)
                     at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
                     at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100)
                     at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176)
                     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
                     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                     ... 237 more
                Caused by: javax.naming.NamingException: Could not dereference object [Root exception is javax.ejb.EJBException: Invalid invocation of local interface]
                     at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1298)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:763)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
                     at javax.naming.InitialContext.lookup(InitialContext.java:392)
                     at org.jboss.seam.Component.instantiateSessionBean(Component.java:1367)
                     at org.jboss.seam.Component.instantiate(Component.java:1331)
                     at org.jboss.seam.Component.newInstance(Component.java:2084)
                     ... 277 more
                Caused by: javax.ejb.EJBException: Invalid invocation of local interface
                     at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:63)
                     at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
                     at org.jnp.interfaces.NamingContext.getObjectInstance(NamingContext.java:1273)
                     at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1290)
                     ... 283 more
                Caused by: java.lang.ClassCastException: org.jboss.ejb3.stateful.StatefulLocalProxyFactory cannot be cast to org.jboss.ejb3.ProxyFactory
                     at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:52)
                     ... 286 more
                



                What does this mean? Do I need other jar libs? Which libs do you have in your war? The StatefulLocalProxyFactory is also in the jboss-ejb3-jar.


                Cornelius

                • 5. Re: Seam and Transactions: How can I rollback?
                  Konstantin Konstantinopel Newbie

                  Apparently, it doesn't solve the problem with my origin exception. If I include the jboss-ejb3.jar I get always the described exception. It doesn't matter if I use '@PersistenceContext private EntityManager em;' or '@In private EntityManager em;'.


                  But if I remove the jboss-ejb3.jar and user '@PersistenceContext private EntityManager em;' it works ... but without the possibility to commit or rollback the transaction (described in my first post).


                  So, what is the problem that the injection of the EntityManager with @In doesn't work? I'm happy about every hint I get!

                  • 6. Re: Seam and Transactions: How can I rollback?
                    Arbi Sookazian Master

                    seam-gen your project-skeleton and entities.


                    then see if it works properly and what the differences are b/n your project and the new one.


                    that's the fastest route.  The exceptions you posted I have never seen before and are most likely due to envmt problems (JBoss version, JARs, etc.) and/or config issues.

                    • 7. Re: Seam and Transactions: How can I rollback?
                      Konstantin Konstantinopel Newbie

                      Now, I've solved the problem. My mistake was the same like described here: http://markmail.org/message/pewzkslx47zzczht


                      I've removed the jar in my webapp that has contained the javax.persistence API and now everything works fine.