Dennis_Byrne@ak.blm.gov
2006-Jun-27 01:42 UTC
[Rails] Transaction syntax for > 1 table/model
If I want to wrap two account updates, the syntax is ... Account.transaction(dennis, david) do dennis.withdraw(10); david.deposit(10); end .. where both dennis and david are instances of Account. Can someone please let me know what the syntax is for starting and ending a *single* database transactions which spans two *different* kinds of models/tables ? Dennis Byrne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060627/7502670c/attachment.html
Hi Dennis, On Jun 26, 2006, at 6:41 PM, Dennis_Byrne@ak.blm.gov wrote:> If I want to wrap two account updates, the syntax is ... > > Account.transaction(dennis, david) do > dennis.withdraw(10); > david.deposit(10); > end > > .. where both dennis and david are instances of Account. > > Can someone please let me know what the syntax is for starting and > ending a *single* database transactions which spans two *different* > kinds of models/tables ?When you start a database transaction it''s in effect for that connection as a whole, not any particular table or row. The Account.transaction and dennis.transaction class and instance methods are simply conveniences which delegate to the current database connection. A.transaction do # begin B.create! # insert into bs (attrs) values (?) C.create! # insert into cs (attrs) values (?) end # commit If you have models whose tables are in different databases and you need transactional guarantees, you''re out of luck - distributed transactions are unsupported. In your example you use an object transaction as well. You rarely want this: if a database transaction rolls back, you''ll want to, say, display dennis.errors. But the object transaction rolls dennis back to his state - including dennis.errors - before the withdrawal. Best, jeremy
Dennis_Byrne@ak.blm.gov
2006-Jun-27 17:23 UTC
[Rails] Transaction syntax for > 1 table/model
> When you start a database transaction it''s in effect for that > connection as a whole, not any particular table or row. The > Account.transaction and dennis.transaction class and instance methods > are simply conveniences which delegate to the current databaseThanks Jeremy, Yes, the transaction is scoped to the entire connection ;) , but the syntax led me to ask if the transaction was somehow restricted to accounts.> In your example you use an object transaction as well. You rarely > want this: if a database transaction rolls back, you''ll want to, say, > display dennis.errors. But the object transaction rolls dennis back > to his state - including dennis.errors - before the withdrawal.I didn''t know this. Thanks again. I will assume it is better to go with a database transaction than an object transaction for this reason. Dennis Byrne -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060627/4b9eb294/attachment.html