Weakness of Transaction in Rails

Dear All:

I have done my test for 2 actions do simultaneously for the same
command. Here is my approvement.


Window DOS 1

F:\ruby\tiesto> ruby test/unit/item_test.rb
Loaded suite test/unit/item_test
Started
.
Finished in 42.078 seconds.

1 tests, 0 assertions, 0 failures, 0 errors


Windows DOS 2

F:\ruby\tiesto>ruby test/unit/item_test.rb
Loaded suite test/unit/item_test
Started
.
Finished in 42.125 seconds.

1 tests, 0 assertions, 0 failures, 0 errors


SCRIPT item_test.rb

require File.dirname( FILE) + ‘/…/test_helper’

class ItemTest < Test::Unit:: TestCase

def test_what
CD = item.find(:first)

Item.transaction do
CD.modification
end

end
end


SCRIPT in Item.rb (Active Record)

class Item < ActiveRecord: :Base

def modification

#delaying process
for z in 1…100000000
end

update_attributes( :stock => stock-1)

end


TABLE BEFORE TESTS RUN


ID | ITEM | QTY |

1 | HEAVEN | 10 |

After 2 Windows Run TEST TOGETHER.


TABLE AFTER EXECUTIONS


ID | ITEM | QTY |

1 | HEAVEN | 9 |

It is not what i expected, it should be 10 - 1 -1 = 8, NOT 9. In other
case i did put Item.transaction do there.

What is your suggestion about it ???

Thank you,
Reinhart
http://teapoci.blogspot.com

CORRECTION TABLE :


ID | ITEM | STOCK |

1 | HEAVEN | 10 |

After 2 Windows Run TEST TOGETHER.


TABLE AFTER EXECUTIONS


ID | ITEM | STOCK |

1 | HEAVEN | 9 |

REGARDS,
Reinhart
http://teapoci.blogspot.com

On May 8, 5:05 pm, Rails T. [email protected]
wrote:

It is not what i expected, it should be 10 - 1 -1 = 8, NOT 9. In other
case i did put Item.transaction do there.

What is your suggestion about it ???

You need locking, not transactions. the problem is that both scripts
load the initial version (ie with quantity = 10). when the second one
completes it ignores the changes made by the first one.

Fred

On May 8, 5:30 pm, Rails T. [email protected]
wrote:

Thanks my bro, fred. Here is my second experiment

Are you tables MyISAM? MyISAM tables don’t support transactions or row
level locks (AR’s optimistic locking is probably a better fit for this
anyway) so this would be entirely surprising (but using transactions
is necessary for pessimistic locking; the lock is held for the
duration of the current transaction

Fred

Yes it is ENGINE=MyISAM :((

How I can move it? I use SQLYOG and XAMP, it says ENGINE=MyISAM, MySQL
GUI v5.14.

Thank you,
Reinhart

On May 8, 6:22 pm, Rails T. [email protected]
wrote:

Yes it is ENGINE=MyISAM :((

How I can move it? I use SQLYOG and XAMP, it says ENGINE=MyISAM, MySQL
GUI v5.14.

set it to innodb. No idea what SQLYOG or XAMP are. Also I noticed that
for some reason you’re doing this from unit tests which (normally)
destroy and reload the test database each time you run them

Fred

Thanks my bro, fred. Here is my second experiment


SCRIPT item_test.rb

require File.dirname( FILE) + ‘/…/test_helper’

class ItemTest < Test::Unit:: TestCase

def test_what

Item.transaction do
CD = item.find(:first, :lock=>true)
CD.modification
end

end
end


DOS in WINDOW 1

F:\ruby\project_dev>ruby test/unit/barang_terpesan_test.rb
Loaded suite test/unit/barang_terpesan_test
Started
.
Finished in 39.734 seconds.

1 tests, 0 assertions, 0 failures, 0 errors


DOS in WINDOW 2

F:\ruby\project_dev>ruby test/unit/barang_terpesan_test.rb
Loaded suite test/unit/barang_terpesan_test
Started
.
Finished in 40.703 seconds.

1 tests, 0 assertions, 0 failures, 0 errors

And No Changes.


TABLE BEFORE EXECUTIONS



ID | ITEM | STOCK |

1 | HEAVEN | 10 |

!!!
TABLE AFTER EXECUTIONS
!!!


ID | ITEM | STOCK |

1 | HEAVEN | 9 |

I have done too with :

ActiveRecord::Base.transaction do
CD = item.find(:first, :lock=>true)
CD.modification
end

And no changes, the result still the same.

Reinhart
http://teapoci.blogspot.com

I cant find to convert MyISAM to InnoDB fast and safety. I have lot of
table tobe converted, any suggestion?

Thanks,
Reinhart

On Thu, May 8, 2008 at 10:44 AM, Rails T.
[email protected] wrote:

I cant find to convert MyISAM to InnoDB fast and safety.

See the MySQL doc for ALTER TABLE


Hassan S. ------------------------ [email protected]

What is the best way to test it out of unit test? Thank you.

On May 8, 6:37 pm, Rails T. [email protected]
wrote:

What is the best way to test it out of unit test? Thank you.

script/runner for example or just require boot.rb and environment.rb

Fred

ALTER TABLE items ENGINE=INNODB

Not work, I use phpMyAdmin at XAMP and SQLYOG