# Rails transactions issue with rollback

**URL:** <https://www.ruby-forum.com/t/rails-transactions-issue-with-rollback/70805>\
**Category:** Rails\
**Created:** [October 3, 2006, 4:24am UTC](https://www.ruby-forum.com/t/rails-transactions-issue-with-rollback/70805 "2006-10-03T04:24:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 3, 2006, 4:24am UTC](https://www.ruby-forum.com/t/rails-transactions-issue-with-rollback/70805/1 "2006-10-03T04:24:58Z")

</div>

Hi,

I have a transaction in which a duplicate key exception is being thrown  
by MySQL. I assume this happens when I try to do a save. The problem  
is that before the save, I do a destroy on another object as part of the  
transaction. Catching the exception by doing this,

User.transaction do  
begin  
objA.destroy  
objB.save \<-- causing exception  
rescue Exception =\> exc  
flash[:notice] = …  
end  
end

does not rollback the destroy. Any help would be appreciated.

Thanks,

Sam

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 3, 2006, 5:22am UTC](https://www.ruby-forum.com/t/rails-transactions-issue-with-rollback/70805/2 "2006-10-03T05:22:34Z")

</div>

Could this be because of using MyISAM tables rather than InnoDB?

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 3, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/rails-transactions-issue-with-rollback/70805/3 "2006-10-03T19:14:23Z")

</div>

On 10/2/06, Sam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a transaction in which a duplicate key exception is being thrown  
> by MySQL. I assume this happens when I try to do a save. The problem  
> is that before the save, I do a destroy on another object as part of the  
> transaction. Catching the exception by doing this,

Rescue the exception _outside_ of the transaction block. Be sure to use  
a  
database that supports transactions (not MySQL + MyISAM.)

begin  
User.transaction do  
objA.destroy  
objB.save!  
end  
rescue Exception =\> exc  
flash[:notice] = …  
end

jeremy
