5 Replies Latest reply on May 31, 2011 3:57 AM by lvdberg

    Transaction does not work

    lfelipeas

      I have a method where I create a new transaction to persist some objects. But when it goes to commit it simply does nothing. Passes by.


      Here is the code:


      @Transactional
           public void insertWithTransaction(PmnGrupoAssistenciaTecnica grupoAstec, PmnProfissionalUtilizado profUtil, 
                     PmnGrupoProfissional grupoPro) {
                Transaction tx = ((Session) (getEntityManager().getDelegate())).getTransaction();
                try {
                     tx.begin();
                     getEntityManager().persist(grupoAstec);
                     getEntityManager().persist(profUtil);
                     getEntityManager().persist(grupoPro);
                     tx.commit();
                }
                catch(Throwable e) {
                     tx.rollback();
                     e.printStackTrace();
                }
           }
      



      Here goes my persistence.xml:


      <?xml version="1.0" encoding="UTF-8"?>
      <!-- Persistence deployment descriptor for dev profile -->
      <persistence xmlns="http://java.sun.com/xml/ns/persistence" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                                            http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
                   version="1.0">
                   
         <persistence-unit name="BL" transaction-type="JTA">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <jta-data-source>java:/BLDatasource</jta-data-source>
            <!-- The <jar-file> element is necessary if you put the persistence.xml in the WAR and the classes in the JAR -->
            <!--
            <jar-file>../../vehicles.jar</jar-file>
            -->
            <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9iDialect"/>
               <property name="hibernate.show_sql" value="true"/>
               <property name="hibernate.format_sql" value="true"/>
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/BLEntityManagerFactory"/>
            </properties>
         </persistence-unit>
          
      </persistence>
      



      And my components.xml:


      <?xml version="1.0" encoding="UTF-8"?>
      <components xmlns="http://jboss.com/products/seam/components"
                  xmlns:core="http://jboss.com/products/seam/core"
                  xmlns:persistence="http://jboss.com/products/seam/persistence"
                  xmlns:drools="http://jboss.com/products/seam/drools"
                  xmlns:bpm="http://jboss.com/products/seam/bpm"
                  xmlns:security="http://jboss.com/products/seam/security"
                  xmlns:mail="http://jboss.com/products/seam/mail"
                  xmlns:web="http://jboss.com/products/seam/web"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:transaction="http://jboss.com/products/seam/transaction"
                  xsi:schemaLocation=
                      "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                       http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.2.xsd
                       http://jboss.com/products/seam/drools http://jboss.com/products/seam/drools-2.2.xsd
                       http://jboss.com/products/seam/bpm http://jboss.com/products/seam/bpm-2.2.xsd
                       http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.2.xsd
                       http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                       http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.2.xsd
                       http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd
                       http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.2.xsd">
      
         <component name="DATE_FORMATTER" class="org.domain.bl.converter.DateFormatter"  auto-create="true" scope="application" >
             <property name="timeZone">#{timeZone}</property>
         </component>
         
         <core:init debug="true" jndi-pattern="@jndiPattern@"/>
      
         <core:manager concurrent-request-timeout="500"
                       conversation-timeout="120000"
                       conversation-id-parameter="cid"
                       parent-conversation-id-parameter="pid"
                       uri-encoding="ISO-8859-1"/>
                    
      
         <!-- Make sure this URL pattern is the same as that used by the Faces Servlet -->
         <web:hot-deploy-filter url-pattern="*.seam"/>
         <web:character-encoding-filter override-client="true" encoding="ISO-8859-1"></web:character-encoding-filter>           
              
              <persistence:managed-persistence-context name="entityManager" auto-create="true"
                            persistence-unit-jndi-name="java:/BLEntityManagerFactory" />                      
      
              <drools:rule-base name="securityRules">
            <drools:rule-files>
               <value>/security.drl</value>
            </drools:rule-files>
              </drools:rule-base>
      
              <security:rule-based-permission-resolver security-rules="#{securityRules}"/>
      
              <security:identity authenticate-method="#{authenticator.authenticate}" remember-me="true"/>
      
              <event type="org.jboss.seam.security.notLoggedIn">
            <action execute="#{redirect.captureCurrentView}"/>
              </event>
              <event type="org.jboss.seam.security.loginSuccessful">
            <action execute="#{redirect.returnToCapturedView}"/>
              </event>
      
           <event type="org.jboss.seam.exceptionHandled">
                <action execute="#{redirect.returnToCapturedView}"/>
           </event>
      
         <mail:mail-session host="localhost" port="25"/>
         
         <!-- For use with jBPM pageflow or process management -->
         <!--
         <bpm:jbpm>
            <bpm:process-definitions></bpm:process-definitions>
            <bpm:pageflow-definitions></bpm:pageflow-definitions>
         </bpm:jbpm>
         -->
      
      </components>
      



      Can somebody help me?


      Thanks!

        • 1. Re: Transaction does not work
          lvdberg

          Hi,


          try to get some experience with the way how Seam takes care of transactions. The Transactional annotation is sufficient as long as the bean is a Seam-managed bean (it must have a Name annotation).


          Let Seam do the hard work for you.


          Leo

          • 2. Re: Transaction does not work
            lfelipeas

            My bean is Seam-managed bean, it has a Name annotation.

            • 3. Re: Transaction does not work
              lvdberg

              Hi,


              I mean that you don't need to include transaction code in your bean. The Transactional should be sufficient, as long as the bean is in scope (is there an active long-running conversation??)


              Leo

              • 4. Re: Transaction does not work
                lfelipeas

                No, there isn't.
                My problem is that my FlushMode was set Manual, and when I was commiting nothing happened.
                So, I set to AUTO and then he did the commit.


                Thanks for your help!


                • 5. Re: Transaction does not work
                  lvdberg

                  Hi Luiz,


                  I don't think you're getting my point at the moment, so a simple example might work better.





                  @Name("yourBean")
                  @Scope(Conversation)
                  public class YourClass implements ...{
                  
                  @In EntityManager entityManager;
                  
                  ... // The rest of your bean
                  @Transactional
                  public void insertWithTransaction(PmnGrupoAssistenciaTecnica grupoAstec, PmnProfissionalUtilizado profUtil, 
                                 PmnGrupoProfissional grupoPro) {
                  
                       entityManager.persist(profUtil);
                       entityManager.persist(grupoPro);
                          entityManager.flush(); // Needed when formanual flush
                  }
                  
                  
                  }



                  If you have set up Seam correctly, this should work.It interception which means, the simple Tranaction annotation cuases Seam to wrap the method call in a Transaction, so there is NO NEED, to interact with the transaction yourself.


                  The (long-running) conversation is something you need to begin yourself, you can add elements in pages.xml, so its done automatically, or you create a create-annotated metghod which you call when you enter the page.


                  Leo


                  P.S. Please read the documentation and look at the examples first before using Seam. Both are really good and give you a good start. If you want more in-depth info, I recommend the Seam in Action book.