Merb + Sequel + mysql transactions

Discussion in 'Ruby' started by Ulag Sriramulu, Apr 21, 2009.

  1. I don't seem to get the transaction to rollback the inserts performed.
    Below are my models

    class Group < Sequel::Model
    one_to_many :members, :class => :Member, :conditions => {:is_member =>
    1}
    one_to_many :non_members, :class => :Member, :conditions =>
    {:is_member => 0}

    def insert
    db.transaction do
    self.save
    self.members.each do |member|
    member.insert
    end
    # force error to check the transaction rolling back
    raise(Sequel::Error::Rollback)

    self.non_members.each do |member|
    member.insert
    end
    end

    end

    end

    class Member < Sequel::Model
    def insert
    db.transaction do
    self.save
    end
    end
    end


    With this code, when I call group.insert I see the below sql in log.
    However, looking in the tables associated, I do see that the group
    record and one member record is inserted. Shouldn't it rollback all the
    inserts in case of errors. I tried model.save:)transaction => true) but
    that did not change this behavior either. Am I missing something?

    2009-04-21T10:00:33.620-07:00|INFO BEGIN
    2009-04-21T10:00:33.620-07:00|INFO INSERT INTO `groups` (`id`) VALUES
    ('group-1')
    2009-04-21T10:00:33.622-07:00|INFO INSERT INTO `members` (`status`,
    `account_id`, `token`, `is_member`, `token_expires_at`, `type`,
    `group_id`) VALUES ('confirmed', '12', NULL, 1, NULL, 'admin',
    'group-1')
    2009-04-21T10:00:33.623-07:00|INFO ROLLBACK
    --
    Posted via http://www.ruby-forum.com/.
     
    Ulag Sriramulu, Apr 21, 2009
    #1
    1. Advertising

  2. Hi Ulag,

    On Tue, Apr 21, 2009 at 10:29 AM, Ulag Sriramulu <> wrote:
    > I don't seem to get the transaction to rollback the inserts performed.
    > Below are my models
    >
    > class Group < Sequel::Model
    > =A0one_to_many :members, :class =3D> :Member, :conditions =3D> {:is_membe=

    r =3D>
    > 1}
    > =A0one_to_many :non_members, :class =3D> :Member, :conditions =3D>
    > {:is_member =3D> 0}
    >
    > =A0def insert
    > =A0 db.transaction do
    > =A0 =A0self.save
    > =A0 =A0self.members.each do |member|
    > =A0 =A0 member.insert
    > =A0 =A0end
    > =A0 =A0# force error to check the transaction rolling back
    > =A0 =A0raise(Sequel::Error::Rollback)
    >
    > =A0 =A0self.non_members.each do |member|
    > =A0 =A0 member.insert
    > =A0 =A0end
    > =A0 end
    >
    > =A0end
    >
    > end
    >
    > class Member < Sequel::Model
    > =A0def insert
    > =A0 =A0 db.transaction do
    > =A0 =A0 =A0 self.save
    > =A0 =A0 end
    > =A0end
    > end
    >
    >
    > With this code, when I call group.insert I see the below sql in log.
    > However, looking in the tables associated, I do see that the group
    > record and one member record is inserted. Shouldn't it rollback all the
    > inserts in case of errors. I tried model.save:)transaction =3D> true) but
    > that did not change this behavior either. Am I missing something?
    >
    > 2009-04-21T10:00:33.620-07:00|INFO BEGIN
    > 2009-04-21T10:00:33.620-07:00|INFO INSERT INTO `groups` (`id`) VALUES
    > ('group-1')
    > 2009-04-21T10:00:33.622-07:00|INFO INSERT INTO `members` (`status`,
    > `account_id`, `token`, `is_member`, `token_expires_at`, `type`,
    > `group_id`) VALUES ('confirmed', '12', NULL, 1, NULL, 'admin',
    > 'group-1')
    > 2009-04-21T10:00:33.623-07:00|INFO ROLLBACK


    The ROLLBACK rolls back everything back to the BEGIN. Looks like it's
    behaving correctly.

    Try posting to the sequel mailing list for a more informed response:
    http://groups.google.com/group/sequel-talk

    jeremy
     
    Jeremy Kemper, Apr 21, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ezra Zygmuntowicz

    [ANN] Merb, Mongrel+Erb

    Ezra Zygmuntowicz, Oct 18, 2006, in forum: Ruby
    Replies:
    0
    Views:
    145
    Ezra Zygmuntowicz
    Oct 18, 2006
  2. Sharon Rosner
    Replies:
    0
    Views:
    158
    Sharon Rosner
    Apr 16, 2007
  3. Eduardo Tongson

    Camping or Merb

    Eduardo Tongson, Sep 11, 2007, in forum: Ruby
    Replies:
    15
    Views:
    192
    Brian Candler
    Oct 14, 2010
  4. Ezra Zygmuntowicz

    [ANN] Merb-0.4.0

    Ezra Zygmuntowicz, Nov 8, 2007, in forum: Ruby
    Replies:
    6
    Views:
    112
    Ezra Zygmuntowicz
    Nov 14, 2007
  5. Roland Swingler

    Problem starting Merb 0.4.1

    Roland Swingler, Nov 24, 2007, in forum: Ruby
    Replies:
    2
    Views:
    130
    Roland Swingler
    Nov 25, 2007
Loading...

Share This Page