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

Hi Dennis,

On Jun 26, 2006, at 6:41 PM, [email protected] wrote:

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

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

Thanks Jeremy,

Yes, the transaction is scoped to the entire connection :wink: , 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