qer*_*oip 18 ruby activerecord transactions ruby-on-rails isolation-level
我需要在每个事务的方式管理的事务隔离级别便携式跨数据库(SQLite的和PostgreSQL,MySQL的至少).
你能推荐一个增强ActiveRecord数据库连接适配器的gem吗?
我知道我可以手动完成,就像那样:
User.connection.execute('SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE')
Run Code Online (Sandbox Code Playgroud)
......但我希望有类似的东西:
User.isolation_level( :serializable ) do
# ...
end
Run Code Online (Sandbox Code Playgroud)
Nob*_*are 29
ActiveRecord本身支持此功能:
MyRecord.transaction(isolation: :read_committed) do
# do your transaction work
end
Run Code Online (Sandbox Code Playgroud)
它支持ANSI SQL隔离级别:
:read_uncommitted
:read_committed
:repeatable_read
:serializable
这个方法从Rails 4开始就可以使用,当OP提出问题时它就不可用了.但对于任何体面的现代Rails应用程序,这应该是要走的路.
Shr*_*ree 12
看起来Rails4将具有开箱即用的功能:
https://github.com/rails/rails/commit/392eeecc11a291e406db927a18b75f41b2658253