Wes Gamble
2007-Jun-27 16:55 UTC
Multi-statement transactions with ActiveRecord (CORRECTED)
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
halfgaar
2007-Jun-27 17:06 UTC
Re: Multi-statement transactions with ActiveRecord (CORRECTED)
On Jun 27, 6:55 pm, Wes Gamble <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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, > WesWhat 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2007-Jun-27 18:08 UTC
Re: Multi-statement transactions with ActiveRecord (CORRECTE
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---