# Transaction not rolled back in Functional Test

**URL:** <https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260>\
**Category:** Rails\
**Created:** [February 10, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260 "2010-02-10T22:17:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [February 10, 2010, 10:17pm UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260/1 "2010-02-10T22:17:42Z")

</div>

Using rails 2.3 and MySQL 5.

I have code in a controller that is something like

begin  
User.transaction do  
user\_object.save

```
do something that might raise an exeception

```

end  
rescue  
Log the error  
give the user the form to try again  
end

It works perfectly when the functionality is executed from the browser  
post. The user is not in the db when the exception is raised.

However, when I run my functional test this the I do a user.find\_by\_name  
after the post is executed and it comes back with the user (should come  
back nil).

I see the exception logged properly but the transaction just is not  
rolled back.

Is this a known issue? Have something to do with the transaction  
created to reset fixture data?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [February 11, 2010, 10:10am UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260/2 "2010-02-11T10:10:35Z")

</div>

On Feb 10, 9:17 pm, Homer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, when I run my functional test this the I do a user.find\_by\_name  
> after the post is executed and it comes back with the user (should come  
> back nil).
> 
> I see the exception logged properly but the transaction just is not  
> rolled back.
> 
> Is this a known issue? Have something to do with the transaction  
> created to reset fixture data?

Tests are special, because each test already runs within a  
transaction. In rails 2.3 Active Record should be using savepoints as  
a substitute for actual nested transactions - do your logs show that  
happening?

Fred

---

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [February 11, 2010, 12:34pm UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260/3 "2010-02-11T12:34:01Z")

</div>

Frederick C. wrote:

> On Feb 10, 9:17ï¿½pm, Homer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Tests are special, because each test already runs within a  
> transaction. In rails 2.3 Active Record should be using savepoints as  
> a substitute for actual nested transactions - do your logs show that  
> happening?
> 
> Fred

What exactly should I be searching for in the log? I don’t see the word  
save point.

---

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [February 11, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260/4 "2010-02-11T15:09:54Z")

</div>

Colin L. wrote:

> On 11 February 2010 11:34, Homer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Delete the log file then run just that test. Then you will see what  
> happened.
> 
> Colin

All I see in the test.log is a few selects, an insert (that should be  
rolled back), the rendering messages, the error I output for the  
exception and the “completed” stats

---

<div class="post-metadata">

**Author:** ![homerlex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bcef8e/32.png) [@homerlex](https://www.ruby-forum.com/u/homerlex)\
**Post date:** [February 11, 2010, 2:31pm UTC](https://www.ruby-forum.com/t/transaction-not-rolled-back-in-functional-test/183260/5 "2010-02-11T14:31:40Z")

</div>

On 11 February 2010 11:34, Homer S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What exactly should I be searching for in the log? Â I don’t see the word  
> save point.

Delete the log file then run just that test. Then you will see what  
happened.

Colin
