Forum: Ruby on Rails Multi-statement transactions with ActiveRecord (CORRECTED)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Wes G. (Guest)
on 2007-06-27 20:55

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")

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.

Wiebe C. (Guest)
on 2007-06-27 21:07
(Received via mailing list)
On Jun 27, 6:55 pm, Wes G. <removed_email_address@domain.invalid>
> 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.
Wes G. (Guest)
on 2007-06-27 22:08
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.

This topic is locked and can not be replied to.