Merb + Sequel + mysql transactions


#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


#2

Hi Ulag,

On Tue, Apr 21, 2009 at 10:29 AM, Ulag S. removed_email_address@domain.invalid
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