Multi-statement transactions with ActiveRecord (CORRECTED)


#1

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


#2

On Jun 27, 6:55 pm, Wes G. 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.


#3

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