假设我们要测试数据库是否被锁定..
$transaction = Thread.new {
Rails.logger.debug 'transaction process start'
Inventory.transaction do
inventory.lock!
Thread.stop
inventory.units_available=99
inventory.save
end
}
$race_condition = Thread.new {
Rails.logger.debug 'race_condition process start'
config = ActiveRecord::Base.configurations[Rails.env].symbolize_keys
config[:flags] = 65536 | 131072 | Mysql2::Client::FOUND_ROWS
begin
connection = Mysql2::Client.new(config)
$transaction.run
$transaction.join
rescue NoMethodError
ensure
connection.close if connection
end
}
Rails.logger.debug 'main process start'
$transaction.join
Rails.logger.debug 'main process after transaction.join'
sleep 0.1 while $transaction.status!='sleep'
Rails.logger.debug 'main process after sleep'
$race_condition.join
Rails.logger.debug 'main process after race_condition.join'
理论上,我认为它会执行事务线程,然后等待( Thread.stop ),然后主进程会看到它正在休眠,并启动竞争条件线程(它将尝试更改锁定表中的数据当它实际工作时)。然后竞争条件将在完成后继续事务线程。
奇怪的是痕迹
main process start
transaction process start
race_condition process start
来自 nodejs,似乎线程并不完全像用户友好.. 但是,必须有一种方法来完成这项工作。
有没有更简单的方法来锁定数据库,然后尝试用不同的线程更改它?