# ActiveRecord Transaction Rollback not happening/Postgres

**URL:** <https://www.ruby-forum.com/t/activerecord-transaction-rollback-not-happening-postgres/183376>\
**Category:** Rails\
**Created:** [February 12, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/activerecord-transaction-rollback-not-happening-postgres/183376 "2010-02-12T17:26:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![smarter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@smarter](https://www.ruby-forum.com/u/smarter)\
**Post date:** [February 12, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/activerecord-transaction-rollback-not-happening-postgres/183376/1 "2010-02-12T17:26:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![smarter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@smarter](https://www.ruby-forum.com/u/smarter)\
**Post date:** [February 12, 2010, 5:45pm UTC](https://www.ruby-forum.com/t/activerecord-transaction-rollback-not-happening-postgres/183376/2 "2010-02-12T17:45:05Z")

</div>

On Feb 12, 4:26 pm, Rohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

Fred

---

<div class="post-metadata">

**Author:** ![smarter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/9fc29f/32.png) [@smarter](https://www.ruby-forum.com/u/smarter)\
**Post date:** [February 12, 2010, 6:03pm UTC](https://www.ruby-forum.com/t/activerecord-transaction-rollback-not-happening-postgres/183376/3 "2010-02-12T18:03:59Z")

</div>

Frederick C. wrote:

> On Feb 12, 4:26ï¿½pm, Rohit S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
> 
> Fred

I 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
