Skip to content

Instantly share code, notes, and snippets.

@yuroyoro
Created September 18, 2014 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuroyoro/c9d65806030f845d96e3 to your computer and use it in GitHub Desktop.
Save yuroyoro/c9d65806030f845d96e3 to your computer and use it in GitHub Desktop.
Fiber使って、ActiveRecord::Base.transactionのblcok内でyieldして、beginとcommit/rollbackを別々の場所で実行するヤツ
fiber = Fiber.new {|commit|
puts "fiber start"
ActiveRecord::Base.transaction {
puts "begin"
Fiber.yield
raise ActiveRecord::Rollback unless commit
puts "commit"
}
puts "fiber end"
}
fiber.resume
puts "in tx"
dosomething_in_transaction()
fiber.resume(false) # rollback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment