Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Last active August 29, 2015 14:01
Show Gist options
  • Save YanhaoYang/c71fbb00c2c28233fda6 to your computer and use it in GitHub Desktop.
Save YanhaoYang/c71fbb00c2c28233fda6 to your computer and use it in GitHub Desktop.
If a Rails server is running in single threading mode, you can begin a transaction by a request / action, then end a transaction with another request / action. All requests between these two requests will be wrapped in one transaction. How can this be useful? Sometimes.
module Concerns
module Transactions
extend ActiveSupport::Concern
def transaction_begin
ActiveRecord::Base.connection.begin_db_transaction
ActiveRecord::Base.connection.increment_open_transactions
render nothing: true
end
def transaction_rollback
ActiveRecord::Base.connection.decrement_open_transactions
ActiveRecord::Base.connection.rollback_db_transaction
render nothing: true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment