5 Replies Latest reply on Feb 4, 2009 8:43 PM by superfis

    flushmode=MANUAL and session.insert() problem

    superfis

      I'm using seam-2.1.2-snapshot and want to use manual hibernate session flushing.
      During long rinning conversation all my operations on managed entities (update, delete) are flushing not until session.flush() exists and it's ok but session.insert() is executed and visible in database immediately and not waiting for session.flush().


      Seam managed sessions are created like this (components.xml):


           <persistence:entity-manager-factory
                name="cmsEntityManagerFactory" persistence-unit-name="cms" />
      
           <persistence:managed-persistence-context
                name="entityManager" auto-create="true" entity-manager-factory="#{cmsEntityManagerFactory}" />
      
           <factory name="session" method="#{entityManager.getDelegate( )}" auto-create="true" />
      




      I'm using seam managed session in my seam action bean:


          @In
          private Session session;
      



      I'm beginning conversation like this:


         @Begin( flushmode = MANUAL, join = true )
          public void begin( ) {
          }
      



      I'm ending conversation in two ways depends on save or cancel button pressed:


         @End
          public String cancel( ) {
              return "admin";
          }
      
      
          @End
          public String save( ) {
              session.flush( );
              return "admin";
          }
      




      I've tried also setting flushmode as MANULA in components.xml (with the same result)


      What's wrong in my approach? There is still unresolved issue JBSEAM-1678 but I don't know if it's directly related to my problem.


      If it's known issue, is there any way to bypass it?


      slawek


        • 1. Re: flushmode=MANUAL and session.insert() problem
          superfis

          I mean ofcourse session.save() not session.insert()... sorry


          Saving part of code looks like:



              public String saveNewItem( ) {
                  PageLanguage pl = new PageLanguage( );
          
                  ... // setting properties of pl
          
                  session.save( pl );
                  return null;
              }



          and is executed as action from ajax4jsf commandButton

          • 2. Re: flushmode=MANUAL and session.insert() problem
            stefanotravelli

            I guess you are working with mysql and autoincrement id.


            In such a case, use persist() instead of save().



            persist() makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.


            save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. identity generator, not sequence), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is problematic in a long-running conversation with an extended Session/persistence context.


            • 3. Re: flushmode=MANUAL and session.insert() problem
              superfis

              Thank you Stefano for you suggestion. It look reasonably. You are right that I'm using mysql with autoincrement.


              I've made changes to have session.persist() instead of session.save() according to your suggestion but still something was wrong and database operations was appearing immediately before session.flush(). I know that it's my mistake somewhere in configuration or so.


              I decided to switch to postgres and all this probles dissapeared - it's working exactly as I expected and all dabatase changes are visible after session.flush()


              Once again, thanks! I'll try to use mysql but now I'm more comfortable working with postgres.


              Slawek

              • 4. Re: flushmode=MANUAL and session.insert() problem
                christian.bauer

                Always use a sequence, or sequence-like generator, if you can (even on MySQL):


                http://in.relation.to/Bloggers/New323HibernateIdentifierGenerators



                • 5. Re: flushmode=MANUAL and session.insert() problem
                  superfis

                  Thanks. I'll try to use sequence (described in blog) with mysql.