6 Replies Latest reply on Dec 22, 2010 6:13 AM by jovincen

    saving a shopping cart state on timeout

    jovincen Newbie

      Hi,


      I am unsure as to the best way to approach this problem. I want to save the conversation state to a DB on timeout instead of destroying them.


      They could then be re-created when the user logs back in. I want to implement this behavior at conversation or entity manager level if possible.


      Any ideas?


      Cheers,
      Jon

        • 1. Re: saving a shopping cart state on timeout
          Leo van den berg Master

          Hi,


          Use the destroy annotation on a specific session scoped method which stores your values in the DB when the session times out.
          When the user logs in again, you can check if there are pending requests and restore these values.


          Leo

          • 2. Re: saving a shopping cart state on timeout
            jovincen Newbie

            Rather then implement specific logic to do this, is it possible to save the entire long running conversation.. or the pending entity changes?


            When the conversation begins then re-load these changes?


            I can implement this type of cache myself as a entityManagerCache but I thought hibernate already does this.

            • 3. Re: saving a shopping cart state on timeout
              Leo van den berg Master

              Hi,


              I am not a persistency expert, but I don't think so, You basically want to save something in a cache (which is non-volatile memory) and restore values when the user comes back. This has a nasty side effect that it will not survive a system shut-down, so you should persist things first. Every Seam time-out can be observerd (because it generates an event or destroy-method call) and that is the right place to add temporary saving things.


              A mopre complex scenario is where this is part of a larger scenario where the user can/must leave the conversation because of the end of a shft/lunch time etc. In such cases you could think of a simple workflow, where the beanstate is saved (this is default bahavior of jBPM). Mind you the latter can become pretty complex.


              Leo     

              • 4. Re: saving a shopping cart state on timeout
                jovincen Newbie

                That is disappointing because the EntityManager caches the changes until the conversation closes. This means they are sitting somewhere in memory. I'd love to get a hold of that delta and persist it as a xml string into a blob field.


                Cheers,
                Jon

                • 5. Re: saving a shopping cart state on timeout
                  Leo van den berg Master

                  Hi,


                  you could make something with an Ajax pollch saves state automatically every n-seconds. In that case there is no need to save it in XML, you just save it the DB. Scenario will have a huge performance impact.


                  Leo

                  • 6. Re: saving a shopping cart state on timeout
                    jovincen Newbie

                    I've written a little object that lets you register what has changed (entities or other). If the conversation ends it maps the objects to XML. When triggered it reloads the state of those objects back from the XML saved.


                    Kinda neat class but would have been nicer if hibernate supported it directly.


                    I am using a similar approach as you suggest, however I wait 60 seconds before making the post and ensure I post maximum every minute. Guess I can make it smarter so it delays the post for every key press. Ie. if you type non stop for 5 minutes it wont post until 6 minutes.. or if you type for 1 minute it will post at 2.
                       This ensures it is on the server, however it wont save to the DB until told.


                    Cheers,
                    Jon