9 Replies Latest reply on Jul 28, 2008 11:25 AM by rhills

    How to use Seam Managed Transaction in POJO.

    holan

      I use Seam without Ejb3.0
      Seam2.0.1GA
      And I use jboss-tools2.0 to generate a new seam-web-project.
      but right now i want to use seam managed transaction in pojo, usually i set the pojo's scope to EVENT( as default in pojo),and i want to use the FlushModeType.Mannual everywhere.But i don't know how to get this.
      Even when i set the pojo's Scope to ScopeType.CONVERSATION,and set the @Begin(flushMode FlushModeType.MANUAL) to one method and the @End to anthoer method.
      It seams like that the flushMode didn't change.
      What should i do?
      any reply is appreciate.

        • 1. Re: How to use Seam Managed Transaction in POJO.
          holan

          can anybody help?

          • 2. Re: How to use Seam Managed Transaction in POJO.
            nickarls

            Show some code and configuration. Are you using a SMPC? Are you injecting the EM using @In?

            • 3. Re: How to use Seam Managed Transaction in POJO.
              holan

              here is my code(I use pojo instead of ejb,and i want to use the FlushMode.Manual)



              @Name("homeBack")
              public class HomeBacking {
                   @In
                   private EntityManager entityManager;
                   @In
                   private SysInformation sys;
              
                   public String insert(){
                        entityManager.persist(sys);
                              System.out.println(entityManager.getFlushMode());
                        return null;
                   }
              }
              


              every time i invoke the insert() method from the xhtml.It print out the flushmode as AUTO.
              I want to use manual everywhere.Even when i use @Scope like this:


              @Name("homeBack")
              @Scope(value=ScopeType.CONVERSATION)
              public class HomeBacking {
                   @In
                   private EntityManager entityManager;
                   @In
                   private SysInformation sys;
                   @Begin(flushMode=FlushModeType.MANUAL,join=true)
                   public String insert(){
                        entityManager.persist(sys);
                        System.out.println(entityManager.getFlushMode());
                        return null;
                   }
                   @End
                   public String update(){
                        return null;
                   }
              }
              


              every time i invoke the insert() method, it immediately insert the record.Shouldn't it do the insert after i invoke the update() method?
              Note that I use pojo,not ejb,does pojo support this?
              here is my component.xml


              <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"/>
                  
                 <persistence:managed-persistence-context name="entityManager"
                                                   auto-create="true"
                                        entity-manager-factory="#{pojoEntityManagerFactory}"/>
              
                 <persistence:entity-manager-factory name="pojoEntityManagerFactory" 
                                    persistence-unit-name="pojo"/>
                 <transaction:entity-transaction entity-manager="#{entityManager}"></transaction:entity-transaction>
                 <drools:rule-base name="securityRules">
                     <drools:rule-files><value>/security.drl</value></drools:rule-files>
                 </drools:rule-base>
              
                 <security:identity authenticate-method="#{authenticator.authenticate}"
                                         security-rules="#{securityRules}"
                                            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>
                 
                 <mail:mail-session host="localhost" port="2525" username="test" password="test" />
              


              here is my web.xml:


              <!-- Ajax4jsf -->
                  
                  <context-param>
                      <param-name>org.richfaces.SKIN</param-name>
                      <param-value>blueSky</param-value>
                  </context-param>
                 
                 <!-- Seam -->
                  
                 <listener>
                    <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
                 </listener>
                  
                  <filter>
                      <filter-name>Seam Filter</filter-name>
                      <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
                  </filter>
               
                  <filter-mapping>
                      <filter-name>Seam Filter</filter-name>
                      <url-pattern>/*</url-pattern>
                  </filter-mapping>
                        
                 <servlet>
                    <servlet-name>Seam Resource Servlet</servlet-name>
                    <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
                 </servlet>
                  
                 <servlet-mapping>
                    <servlet-name>Seam Resource Servlet</servlet-name>
                    <url-pattern>/seam/resource/*</url-pattern>
                 </servlet-mapping>
                 
                 <!-- Facelets development mode (disable in production) -->
                 
                 <context-param>
                    <param-name>facelets.DEVELOPMENT</param-name>
                    <param-value>true</param-value>
                 </context-param>
                  
                 <!-- JSF -->
                 
                 <context-param>
                    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                    <param-value>.xhtml</param-value>
                 </context-param>
              
                 <servlet>
                    <servlet-name>Faces Servlet</servlet-name>
                    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                    <load-on-startup>1</load-on-startup>
                 </servlet>
                  
                 <servlet-mapping>
                    <servlet-name>Faces Servlet</servlet-name>
                    <url-pattern>*.seam</url-pattern>
                 </servlet-mapping>
                                
                 <security-constraint> 
                     <display-name>Restrict raw XHTML Documents</display-name>
                     <web-resource-collection>
                         <web-resource-name>XHTML</web-resource-name>
                         <url-pattern>*.xhtml</url-pattern>
                     </web-resource-collection>
                     <auth-constraint/>
                 </security-constraint>
              



              thank for you all.
              any help is appreciate;

              • 4. Re: How to use Seam Managed Transaction in POJO.
                nickarls

                Section 27.4. Annotations for context demarcation in the manual mentions


                @Begin
                
                Specifies that a long-running conversation begins *when this method returns a non-null outcome* without exception. 
                


                • 5. Re: How to use Seam Managed Transaction in POJO.
                  holan

                  thanks for you help really;It really help me a lot.And I'm sorry I didn't read the reference very carefully.
                  I change my code like this.


                  @Name("homeBack")
                  @Scope(value=ScopeType.CONVERSATION)
                  public class HomeBacking {
                       @In
                       private EntityManager entityManager;
                       @In
                       private SysInformation sys;
                       @Begin(flushMode=FlushModeType.MANUAL,join=true)
                       public String insert(){
                            System.out.println(entityManager.getFlushMode());
                            System.out.println(entityManager);
                            return "self";
                       }
                       @End
                       public String update(){
                            System.out.println(entityManager);
                            return "self";
                       }
                  }
                  


                  here self means navigate to the same page.Now the System.out.println(entityManager) in both insert() and update() method does print the same entityManager instance.but the System.out.println(entityManager.getFlushMode()) also print the FlushMode.AUTO.
                  i want to use the FlushMode.Manual type.And I want to use Manual even in the ScopeType.Event mode. How can i do this.
                  thank you very much.

                  • 6. Re: How to use Seam Managed Transaction in POJO.
                    nickarls

                    It might be that the annotation is processed at a later point so I'm not sure if checking it inside the method is accurate. Try placing debug output on your page to see if you are really in a long running conversation and what the flush mode really is...

                    • 7. Re: How to use Seam Managed Transaction in POJO.
                      holan

                      should i set the  transaction-type JTA in the persistence.xml.
                      Right now it is Resource-local


                      and the datasource.xml


                      I use   local-tx-datasource
                      should i change it with xa-datasource
                      ?


                      • 8. Re: How to use Seam Managed Transaction in POJO.
                        holan

                        I want to know that if choose seam without ejb, I can still use the FlushMode.Manual in ScopeType.Conversation or ScopeType.event or any scope else.
                        or I should change back to ejb.
                        but debug in ejb is so painful....
                        thank you very much

                        • 9. Re: How to use Seam Managed Transaction in POJO.
                          rhills

                          Hi,


                          I'm still quite new at Seam, but from my experimentation in this area, if you join an existing transaction whose flushMode is AUTO, it doesn't appear that you can change it with your @Begin annotation.


                          I got around this by using nesting, ie:


                          @Begin(flushMode=FlushModeType.MANUAL,nested=true)
                          



                          HTH,


                          Rob Hills