Forum: Ruby Merb + Sequel + mysql transactions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
348c8154267fc50bd966341d31f75634?d=identicon&s=25 Ulag Sriramulu (ulag)
on 2009-04-21 19:28
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
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 Jeremy Kemper (Guest)
on 2009-04-21 21:41
(Received via mailing list)
Hi Ulag,

On Tue, Apr 21, 2009 at 10:29 AM, Ulag Sriramulu <ulag@hotmail.com>
wrote:
>   db.transaction do
>   end
>  end
> 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
This topic is locked and can not be replied to.