# Could someone explain this (transaction-related)?

**URL:** <https://www.ruby-forum.com/t/could-someone-explain-this-transaction-related/136426>\
**Category:** Rails\
**Created:** [May 1, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-transaction-related/136426 "2008-05-01T23:39:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 1, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-transaction-related/136426/1 "2008-05-01T23:39:07Z")

</div>

Hi all,

I was testing, and found a nested transaction (which I know is bad), and  
there’s something I don’t understand. If an exception occurs during a  
transaction, it is supposed to roll back. In a nested transaction,  
where an exception is raised out of the ‘inner’ transaction, this  
doesn’t occur. However, if I catch the exception and manually call  
‘connection.rollback\_db\_transaction’, it works, e.g.:

Applicant.transaction do  
a = Applicant.find(1)  
begin  
Applicant.transaction do  
a.first\_name = ‘bar’  
a.save!  
raise “Foo” # This does not roll back the transaction…  
end  
rescue  
Applicant.connection.rollback\_db\_transaction # But this will  
end  
end

I don’t see why it makes a difference if I call rollback\_db\_transaction  
in rescue. I thought that happened automatically. Looking at the  
source, I can’t see where it happens.

Thanks if anyone can clear this up for me. I am trying to use  
transactional fixtures for all my tests, and this is a sticking point.  
If I can understand this, I think I can convert them all.

---

<div class="post-metadata">

**Author:** ![kuma](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@kuma](https://www.ruby-forum.com/u/kuma)\
**Post date:** [May 1, 2008, 11:51pm UTC](https://www.ruby-forum.com/t/could-someone-explain-this-transaction-related/136426/2 "2008-05-01T23:51:51Z")

</div>

On 1 May 2008, at 22:39, Brian H. wrote:

> 

Rails doesn’t support nested transactions, so only the outer one is  
actually doing anything, by rescuing the exception you are preventing  
rails from doing the rollback.

Fred
