Wrap multiple requests in one transaction
If a Rails server is running in single threading mode, you can begin a transaction by a request, then end a transaction with another request. 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
Written by yanhaoyang
Related protips
1 Response
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/73096/6b67518d6700874714698702879014a2.jpeg)
Is this still relevant?
I was thinking about having test-transaction-like functionality in development mode;
Like – submit a form, see what happens, and rollback by clicking some button.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Activerecord
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#