# If one has a nested transaction does the outer-most transaction take precedence?

**URL:** <https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528>\
**Category:** Rails\
**Created:** [February 5, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528 "2009-02-05T12:48:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 5, 2009, 12:48pm UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/1 "2009-02-05T12:48:16Z")

</div>

Hi,

Anyone know, if I have a nested transaction, will it be the outer most  
transaction (i.e. from outermost transaction start, to outermost  
transaction end) that will be used?

i.e. noting Rails doesn’t currently support nested transactions.  
Another way to ask would be to say, if there was a database statement  
error between the end of the innermost transaction and the end of the  
outmost transaction, would the innermost transaction database  
statements be rolled back?

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 5, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/2 "2009-02-05T13:52:59Z")

</div>

On 5 Feb 2009, at 11:47, Greg H. wrote:

> outmost transaction, would the innermost transaction database  
> statements be rolled back?

THis is probably database dependant but on mysql at least a nested  
begin will commit the existing transaction and begin a new one (which  
is why rails is careful not to start a new actual transaction in this  
case). rails 2.3 supports savepoints for what it’s worth.

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 5, 2009, 9:04pm UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/3 "2009-02-05T21:04:27Z")

</div>

thanks - so this means for the moment at least (until v2.3) one has to  
be careful re having a method with a transaction begin, that within  
may call another method that itself has a transaction begin. So  
really group your methods into ones that use & don’t use transactions  
explicitly I guess…

2009/2/5 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 5, 2009, 11:43pm UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/4 "2009-02-05T23:43:49Z")

</div>

On 5 Feb 2009, at 20:03, Greg H. wrote:

> thanks - so this means for the moment at least (until v2.3) one has to  
> be careful re having a method with a transaction begin, that within  
> may call another method that itself has a transaction begin. So  
> really group your methods into ones that use & don’t use transactions  
> explicitly I guess…

You’re ok if you’re not doing things by hand: if you do

Foo.transaction do  
Foo.transaction do  
…  
end  
end

then the inner call to transaction is basically a no-op.

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 6, 2009, 2:24am UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/5 "2009-02-06T02:24:40Z")

</div>

On 6 Feb 2009, at 00:25, Greg H. wrote:

> end

Correct. The above is equivalent to

Foo.transaction do  
statement A  
statement B  
end

(Again assuming you’re not using 2.3’s savepoint support (which is  
turned off by default I believe))

Fred

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [February 6, 2009, 1:26am UTC](https://www.ruby-forum.com/t/if-one-has-a-nested-transaction-does-the-outer-most-transaction-take-precedence/159528/6 "2009-02-06T01:26:10Z")

</div>

ok - so just to be clear then, statement A will rollback if there is  
an issue with the statement B? this is at least good then for me

Foo.transaction do  
Foo.transaction do  
statement A  
…  
end  
statement B  
end

2009/2/6 Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> Fred
> 
> > > > Anyone know, if I have a nested transaction, will it be the outer  
> > > > statements be rolled back?  
> > > > Greg  
> > > > –  
> > > > Greg  
> > > > [http://blog.gregnet.org/](http://blog.gregnet.org/)
> > 
> > >
> 
> >

–  
Greg  
[http://blog.gregnet.org/](http://blog.gregnet.org/)
