Merb + Sequel + mysql transactions

U

Ulag Sriramulu

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
 
J

Jeremy Kemper

Hi Ulag,

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top