3 Replies Latest reply on Feb 18, 2009 2:35 PM by Gregory Nikle

    How to rollback transaction in Seam?

    Gregory Nikle Apprentice

      Hi,
      Can anybody tell me how to rollback transaction in Seam?


      I use:



           <persistence-unit name="SGUniAdmDatabase" transaction-type="JTA">
               <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <jta-data-source>java:/SGUniAdmDatasource</jta-data-source>
                     <class>pl.administration.model.Adv_Links</class>          
                     <class>pl.administration.model.Adv_Emails</class>
                     <class>pl.administration.model.News</class>
                     <class>pl.administration.model.Pacc_Passwords</class>
                     <class>pl.administration.model.Tutorial</class>
                     <exclude-unlisted-classes>true</exclude-unlisted-classes>               
                     <properties>
                          <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                         <!-- 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="hibernate.cache.use_second_level_cache" value="true" /> 
                       <property name="hibernate.cache.use_query_cache" value="true"/>
                       <property name="hibernate.cache.region_prefix" value="hibernate.test"/>
                         <property name="hibernate.cache.use_minimal_puts" value="true"/>
                          <property name="hibernate.cache.use_structured_entries" value="true"/>
                          
                       <property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
                         <!-- 
                               <property name="hibernate.generate_statistics" value="false"/>
                               <property name="hibernate.connection.isolation" value="4"/>   -->
                            
                                    
                          <!-- <property name="hibernate.show_sql" value="true"/> -->
                          <!-- <property name="hibernate.hbm2ddl.auto" value="create"/>  -->                    
                          
                          <property name="hibernate.transaction.manager_lookup_class" 
                                    value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
                          <property name="jboss.entity.manager.factory.jndi.name" 
                                    value="java:/SGUniAdmEntityManagerFactory" />
                     </properties>
           </persistence-unit>



      and



           @In
           private EntityManager sguniAdm;    



      here is try block:


      try {
      ....some error
      } catch (Exception e) {
       sguniAdm.???????
      }
      




        • 1. Re: How to rollback transaction in Seam?
          Binesh Gummadi Novice

          You can throw an @ApplicationException with rollback set to true and handle the exception in pages.xml. May be display the same page with error message.

          • 2. Re: How to rollback transaction in Seam?
            Gregory Nikle Apprentice

            Hi,
            I read about rollingback exceptions and found some example in Seam blog example.


            Can u point or correct me is that right?
            In my source code, I've got lot of sub methods/classes and need to service that and keep atomic transaction.


            This is my exception.


            import javax.ejb.ApplicationException;
            import javax.servlet.http.HttpServletResponse;
            
            import org.jboss.seam.annotations.exception.HttpError;
            
            @ApplicationException(rollback=true)
            @HttpError(errorCode=HttpServletResponse.SC_NOT_FOUND)
            public class SGUniUncheckedException extends Exception {
                 
                 SGUniUncheckedException(String id) {
                      super(id);
                 }
            }



            In my case, I don't want to show user that error is thrown or any, and I don't think adding to pages.xml anythink is necessary.


            and my app method


            @TransactionAttribute(TransactionAttributeType.MANDATORY)
            private void partOne() {
               try {
                   //do somethink on DB
               } catch (SGUniUncheckedException e) {
                   throw e;
               }
            }




            upper method which will rollback exception;


            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
            public void mainMethod() {
                try {
                    ///do somethink
                    partOne();
                } catch (SGUniUncheckedException e) {
                    throw e;
                }
            
            }
            




            • 3. Re: How to rollback transaction in Seam?
              Gregory Nikle Apprentice

              Can anybody help me with that problem, or show own source code to point on right way?
              Please for help