# Ask about Transaction

**URL:** <https://www.ruby-forum.com/t/ask-about-transaction/137214>\
**Category:** Rails\
**Created:** [May 10, 2008, 6:06am UTC](https://www.ruby-forum.com/t/ask-about-transaction/137214 "2008-05-10T06:06:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 10, 2008, 6:06am UTC](https://www.ruby-forum.com/t/ask-about-transaction/137214/1 "2008-05-10T06:06:33Z")

</div>

Dear ALL"

I would like to ask, what is it true that rails already supports  
transaction although we do not put ActiveRecord::Base.transaction in our  
ruby controller or model script? - My DB ENGINE is InnoDB -

I did test in web browser with putting delay process inside of script to  
make 2 concurrent process having collision, but the fact at final, they  
are handled with transaction, i do not put any  
ActiveRecord::Base.transaction or ModelName.transaction script there.

Example :  
They are 10 items in stock. 2 concurrent process wanted to take 1 item  
from stocks. If it is not having ActiveRecord::Base.transaction in  
method the result would be 9, but the fact is it is 8 (10-1-1). Look the  
tables process below without transaction in console (table 1) & web  
browser (table 2):

[http://pastie.caboo.se/194711](http://pastie.caboo.se/194711)

How come my test in console and web browser are difference ??

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 10, 2008, 6:51pm UTC](https://www.ruby-forum.com/t/ask-about-transaction/137214/2 "2008-05-10T18:51:08Z")

</div>

On 10 May 2008, at 05:06, Rails T. wrote:

> Dear ALL"
> 
> I would like to ask, what is it true that rails already supports  
> transaction although we do not put ActiveRecord::Base.transaction in  
> our  
> ruby controller or model script? - My DB ENGINE is InnoDB -

I’m not quite sure what you’re asking, but rails does wrap the  
validation, before\_save, save and after\_save sequence in a transaction.

> method the result would be 9, but the fact is it is 8 (10-1-1). Look  
> the  
> tables process below without transaction in console (table 1) & web  
> browser (table 2):
> 
> [Parked at Loopia](http://pastie.caboo.se/194711)
> 
> How come my test in console and web browser are difference ??  
> If you’re just running a single mongrel/webrick instance then when you  
> test with your browser you’re not actually executed 2 things at the  
> same time (since a rails instance only handles one request at a time).

Fred  
(PS: if you want to insert a delay, the sleep method is rather handier  
than counting to a million)

---

<div class="post-metadata">

**Author:** ![malioboro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/eb8c5e/32.png) [@malioboro](https://www.ruby-forum.com/u/malioboro)\
**Post date:** [May 10, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/ask-about-transaction/137214/3 "2008-05-10T21:33:18Z")

</div>

My Fred Brother thank you for your answer. Sleep mode is good idea and  
also you give me information about :

> If you’re just running a single mongrel/webrick instance then when you  
> test with your browser you’re not actually executed 2 things at the  
> same time (since a rails instance only handles one request at a time).

I test : I open 2 web browser and do concurrent process by accessing the  
same method for adding 1 item to cart, but it looks like having  
transaction in my method, whereas I do not put  
ActiveRecord::Base.transaction there. Since you explained above, now i  
understand why my concurent process looks like having transaction,  
because it handles one request at a time and other can only follow.

Thank you,  
Reinhart  
[http://teapoci.blogspot.com](http://teapoci.blogspot.com)
