Using transactions with Ruby DataMapper

January 21st, 2010 by Ludo van den Boom

With DataMapper, it can be quite a challenge to perform tasks that seem so simple at first. DataMapper is an object-relational mapper (ORM) written in Ruby, with a lot of great features and innovative approaches to common ORM-tasks. But it lacks documentation for some of the less-used features and even the more popular search engines don’t have the answer to all questions.

Note: the following examples have been tested with DataMapper version 0.10.2.

One of the questions I had was how to perform multiple queries within a single (database) transaction, so that I can ‘stage’ a number of database queries and commit (or rollback) them all in one go. Fortunately, after some investigation of the DataMapper source code, it turns out to be an extremely simple task:

User.transaction do
  User.first.update(:name => "John Smith")
  Address.first.update(:street => "") # Invalid: street must not be blank
end

With this code neither the address nor the user will be updated in the database if one of the updates fails. The syntax is actually the same as for the ActiveRecord ORM. However, triggering a rollback is done differently, as can be seen in the following example.

User.transaction do |t|
  User.first.update(:name => "John Smith")
  Address.first.update(:street => "Oak St. 400")
  t.rollback # Transaction will be rolled back even when updates are OK.
end

That’s it for now. I’m sure there are some additional features that I haven’t mentioned here, but this is all I had to know at this point.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • Hyves
  • Reddit
  • StumbleUpon
  • Twitter
  • http://ariejan.net Ariejan

    To mimic AR’s behaviour, you could do the following

    User.transaction do |t|
      begin
        User.first.update(:name => "John Smith")
        Address.first.update(:street => "Oak St. 400")
      rescue
        t.rollback
      end
    end
  • http://www.4.com Valentin Burdis

    What exactly I wouldnt give to get any debate with you about this. You simply say a great number of elements that arrive from nowhere that Im somewhat particular Id have a very fair shot. Your weblog is wonderful visually, I imply men and women wont be bored. But many others who can see previous the videos and also layout wont be so impressed jointly along with your generic knowledge of this topic.