6 Replies Latest reply on Jan 29, 2007 8:49 AM by gavin.king

    How to avoid implicit database update

    umarzubair


      I am using hibernate with Seam.
      I have a Conversation scope Java Bean. In first request I get a record from database if i change some value of this record. Application update this record implicitly. I am not using update or persist for that record. I want to know how to avoid this update in database. I want to update database at last step of transaction.

      I am using Seam1.1.0 GA and hibernate 3.2. May be i have to change some configuration at hibernate level, but I am not sure about that.

      Please let me know the solution.

      Thanks

        • 1. Re: How to avoid implicit database update
          gavin.king

          Use an atomic conversation @Begin(flushMode=MANUAL)

          • 2. Re: How to avoid implicit database update
            umarzubair

            Thanks for your quick reply.

            Yes, it worked but I have to call commit or flush explicitly. I am using TransactionManager.
            I just want to avoid dirty updates.
            For example If I have a conversation based on 2 requests. In first request Application update a data model, and in second request Application calls session.saveOrUpdate method. I just want to update database after second request by Seam Transaction Manager.


            Umar

            • 3. Re: How to avoid implicit database update
              umarzubair

              Now I am using your recommendation
              @Begin(flushMode=MANUAL) and at end of conversation calling session.flush() explicitly.

              Now I have another problem. Suppose 1 conversation has 2 requests.
              In request 1, Application fetch a record say 'A' from database.
              Before second request, Record 'A' gets updated by some other transaction.
              In request 2, Application need to execute same query to find record 'A' again with latest data. HibernateSession does not go into database to find latest values for Record 'A' and it return same record 'A' fetched in request 1.

              If I use session.evict(model) in request 1, then request 2 returns latest record from database.

              Is there any way to get latest record from database every time without using evict method.

              Regards,
              Umar

              • 4. Re: How to avoid implicit database update
                gavin.king

                No, you should use optimistic locking, ie. add a @Version attribute to your entity.

                • 5. Re: How to avoid implicit database update
                  umarzubair

                  I am already using version element in hbm file.


                  It does not solve the issue.

                  Umar

                  • 6. Re: How to avoid implicit database update
                    gavin.king

                    Then you are fine. There is no issue to solve. This is the correct way to process long txns in an online app.