Skip to content

Instantly share code, notes, and snippets.

@kevn
Created May 25, 2011 20:51
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 kevn/991935 to your computer and use it in GitHub Desktop.
Save kevn/991935 to your computer and use it in GitHub Desktop.
def fuck_my_transaction
begin
ActiveRecord::Base.connection.begin_db_transaction
# Raise any error except an AR::StatementInvalid, such as an ActiveRecord::Rollback,
# which is generally used to signal a rollback condition
raise ActiveRecord::Rollback.new("Fucking your transaction.")
rescue ActiveRecord::StatementInvalid
puts "Rescued, rolling back..."
ActiveRecord::Base.connection.rollback_db_transaction
end
ensure
puts "Outside block; the connection is still in a transaction if this is 1: #{ActiveRecord::Base.connection.instance_variable_get(:@nested_transaction_count)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment