Rohit Sachdeva
2010-Feb-12 16:26 UTC
ActiveRecord Transaction Rollback not happening/Postgres
I have these settings using postgresql gem with Postgres 8.4 Installed:
development:
adapter: postgresql
host: localhost
database: db_development
username: postgres
password: postgres
encoding: utf8
Code snippet in lib/ rake task:
Benchmark.bm(7) do |x|
res = x.report("localdb:populate") do
ruby_obj = YAML.load_file("lib/tasks/populatedb.yml")
ruby_obj.each_key do |key|
h = ruby_obj[key]
begin
ActiveRecord::Base.transaction do
msg = Message.new
msg.name= h[''name'']
msg.message_key= h[''message_key'']
msg.category= h[''category'']
msg.displayable=true if h[''displayable'']
msg.save
end
rescue => e
logger.info "Error happened #{e.backtrace}"
end
end
end
the populatedb.yml just has some yml Message key.
Message is a ActiveRecord Object.
It has validation on message_key.
I intentionally want that either all messages get populated or none and
to check that I make message_key same for 2 messages with
validates_uniqueness_of :message_key.
My problem is that even is the same key exists in populatedb.yml data,
everything works well.
No rollback.
Is this code correct?
ActiveRecord::Base.transaction do
msg = Message.new
msg.name= h[''name'']
msg.message_key= h[''message_key'']
msg.category= h[''category'']
msg.displayable=true if h[''displayable'']
msg.save
end
Do I need to do anything special in Postgres Installation DB Set up?
Does the postgres adapter handle this situation?
Thanks in advance.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Feb-12 16:43 UTC
Re: ActiveRecord Transaction Rollback not happening/Postgres
On Feb 12, 4:26 pm, Rohit Sachdeva <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> > My problem is that even is the same key exists in populatedb.yml data, > everything works well. > No rollback. > > Is this code correct? > ActiveRecord::Base.transaction do > msg = Message.new > msg.name= h[''name''] > msg.message_key= h[''message_key''] > msg.category= h[''category''] > msg.displayable=true if h[''displayable''] > msg.save > end > > Do I need to do anything special in Postgres Installation DB Set up? >Well your transaction only wraps a single insert, so only that insert could get rolled back. Furthermore, save doesn''t raise an exception if saving fails (save! does) so you''d never get a rollback anyway. Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Smart RoR
2010-Feb-12 17:03 UTC
Re: ActiveRecord Transaction Rollback not happening/Postgres
Frederick Cheung wrote:> On Feb 12, 4:26�pm, Rohit Sachdeva <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > >> � � � � � � � msg.category= h[''category''] >> � � � � � � � msg.displayable=true if h[''displayable''] >> � � � � � � � msg.save >> � � � � � � end >> >> Do I need to do anything special in Postgres Installation DB Set up? >> > Well your transaction only wraps a single insert, so only that insert > could get rolled back. Furthermore, save doesn''t raise an exception if > saving fails (save! does) so you''d never get a rollback anyway. > > FredI actually changed this as follows after the post and it does work. The ROLLBACK shows in the log also. Thanks Fred though. SIlly to overlook it. Also, the ROLLBACK happens at the validation level itself. I don''t have to throw the exception or even catch it. begin ActiveRecord::Base.transaction do ruby_obj.each_key do |key| h = ruby_obj[key] msg = Message.new msg.name= h[''name''] msg.message_key= h[''message_key''] msg.category= h[''category''] msg.displayable=true if h[''displayable''] msg.save! end end rescue => e logger.info "Error happened #{e.backtrace}" #throw e end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.