3 Replies Latest reply on Nov 26, 2014 11:31 AM by Jon Tara

    Do transactions span Models?

    Tom Carvin

      This sample is from the online docos

       

      db = ::Rho::RHO.get_src_db('Model')

        db.start_transaction

        begin

        items.each do |item|

        # create hash of attribute/value pairs

        data = {

        :field1 => item['value1'],

        :field2 => item['value2']

        }

        # Creates a new Model object and saves it

        new_item = Model.create(data)

        end

        db.commit

        rescue

        db.rollback

        end



      My question is, does that transaction span models such that I could create multiple new records of several dependent model types and, in the face of an eror, do a single rollback to revert them at once?

        • Re: Do transactions span Models?
          Kutir Mobility

          Hi Tom,

          Yes it can revert all pending actions before a commit via single rollback command.

          For latest version Please refer the link http://docs.rhomobile.com/en/5.0.0/api/Database#mrollbackTransaction

           

          Visnupriya R

          Kutir Mobility

          1 of 1 people found this helpful
            • Re: Do transactions span Models?
              Tom Carvin

              I wish this project was using the latest version, but alas that is not the case.  The latest API definitely makes it clear that the transaction spans all work againt the DB.  The older API places the transaction API at the model level instead, so this is less clear.  I didn't find anything definitive in the older docos.

                • Re: Do transactions span Models?
                  Jon Tara

                  Tom - it takes very little effort to go from Rhodes 4.x. to 5.x. The big jump is from 3.5.x to 4.x. The old 3.5 APIs are still there, and so there is no need to re-write, for example, your network code. Use new features in new code, leave the old code if you can't get to updating it.

                   

                  We went from 3.5 to 4.0 in a couple weeks, (for a suite of 6 large apps) and then from 4.x to 5.x in a day. There is very little if anything you would need to change.

                   

                  Of course, if there's something broken in 5 that you need and know about, that's a different matter. I'd urge you to update, otherwise.

                   

                  We wanted to take advantage of certain features, and so I re-wrote our application.rb to use new callbacks, and we re-wrote all of our network code, because we were setting timeouts globally and now can set them on each network call. Otherwise, there would have been no immediate need to re-write network code. I changed our code to use new logging, because of the better flexibility.

                   

                  I suppose eventually they will pull the plug on old APIs but for now it is easy to triage and only change code where there is some direct benefit to changing it.

                   

                  ----

                  I took a look at some old code of mine that uses transactions, but that wasn't helpful, since that code only uses one table!

                   

                  The new API does certainly make it more clear that it applies across tables.

                   

                  It's not all *that* unclear int he old one, though, when you read the code:

                   

                  db = ::Rho::RHO.get_src_db('Model')


                  So, you're getting the database object that holds some model. And then you are working with that database. The difference in 4.x is now there is a convenient global database instance that you can reference directly instead of asking a model for it. The new API is certainly a better, more clear and consistent design.