̃Gg[͂ĂȃubN}[Nɒlj

Ruby :: イディオム / 例外処理&トランザクション



#!/usr/local/bin/ruby -Ke
 
print "Content-type: text/html; charset=EUC-JP\n\n"
 
sql1 = "insert into teble_1 (num, str) values(123, 'abc')"
sql2 = "insert into table_2 (num, str) values(789, 'xyz')"
 
# DBインスタンスが生成されることを想定
db = DB.get
 
begin
	f=0
	db.exec("begin")
	db.exec(sql1)
	f=1
	db.exec(sql2)
	db.exec("commit")
rescue => e
	print e.to_s
	print "<br>rescue<br>"
	print "f:#{f}"
ensure
	print "<br>ensure<br>"
	print "f:#{f}"
end

上記スクリプトを「sql2」が”成功した場合”と”失敗した場合”の両方で実行して、それぞれの場合で teble_1 と teble_2 の更新状態を比較する。




programming/ruby/idiom/exception_transaction.txt