There does not seem to be a global isolation option, thus you are left with four options:
- Monkeypatch existing
transaction
implementation, so that it picks
your desired isolation level. (Monkeypatching is not desirable)
Use correct isolation
level throughout your application.
SomeModel.transaction(isolation: :read_committed)
Extend ActiveRecord
and create your own custom transaction method.
As commented - you may be able to edit the default isolation level in DB configuration. For postgres it's this one
Example code:
#lib/activerecord_extension.rb
module ActiveRecordExtension
extend ActiveSupport::Concern
module ClassMethods
def custom_transaction
transaction(isolation: :read_committed) do
yield
end
end
end
end
ActiveRecord::Base.send(:include, ActiveRecordExtension)
Then in your initializers:
#config/initializers/activerecord_extension.rb
require "active_record_extension"
Afterwards you can use:
MyModel.custom_transaction do
<...>
end
And in the future, this will allow you to change the isolation level in one place.