# Multi-statement transactions with ActiveRecord (CORRECTED)

**URL:** <https://www.ruby-forum.com/t/multi-statement-transactions-with-activerecord-corrected/107942>\
**Category:** Rails\
**Created:** [June 27, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/multi-statement-transactions-with-activerecord-corrected/107942 "2007-06-27T18:55:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 27, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/multi-statement-transactions-with-activerecord-corrected/107942/1 "2007-06-27T18:55:26Z")

</div>

All,

I didn’t finish writing this before I posted the previous post. Sigh.

Using Rails 1.1.6  
AR against MS SQL Server (ODBC mode).

I need to do some fancy bulk inserting of data, which I need to ensure  
is within a transaction. But as I think about it, it isn’t clear to me  
whether what I want to do will work within standard AR.

If I do something like this:

MyClass.transaction do  
MyClass.connection.execute(“arbitrary SQL”)

…  
MyClass.connection.execute(“arbitrary SQL”)  
end

will this all use the same connection and actually work within the  
transaction, even though I’m explicitly doing execute on  
MyClass.connection _within_ the transaction? I’m concerned that using  
MyClass.connection implicitly opens a new connection.

Can anyone verify whether or not this is the case?

If I can’t rely on this, I think I should be able to just write up my  
SQL with explicit SQL-level transaction management within it, and pass  
that to one call to MyClass.connection.execute.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 27, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/multi-statement-transactions-with-activerecord-corrected/107942/2 "2007-06-27T19:07:31Z")

</div>

On Jun 27, 6:55 pm, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> transaction, even though I’m explicitly doing execute on  
> Wes  
> What exactly is myclass in this context? Just a AR::Base based class?  
> I execute SQL with ActiveRecord::Base.connection.execute(), and it  
> keeps it within a transaction. If you need to be sure, check out log/  
> development.log in your project dir, and see what SQL it generates.  
> You should see BEGIN; and COMMIT: around the block of statements.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [June 27, 2007, 8:08pm UTC](https://www.ruby-forum.com/t/multi-statement-transactions-with-activerecord-corrected/107942/3 "2007-06-27T20:08:07Z")

</div>

MyClass is an AR::Base descendant, correct.

My concern is that the execute statements will use a new connection and  
not be subject to the transaction block in which they are placed.

Thanks,  
Wes
