问题
I want to make my application serialize every transaction by default. I'd then relax isolation based on performance measurements and knowing what data particular actions/transactions use and change.
I doubt serializable by default would get into the framework, as it'd slow things down and be difficult to explain. But I don't want to deal with db corruption, and do want internally consistent aggregate calculations.
For case-by-case isolation levels there is Rails postgresql how to set transaction isolation level to serializable but I think this approach is wrong for the same reasons html-escaping to protect against xss were wrong and were dropped: whitelisting is safer than blacklisting.
Has anyone done this already? Where would be a good place to hook into rails to do this?
回答1:
If you want this the default, then I suggest setting it in your MySQL config. The setting is described here: https://dev.mysql.com/doc/refman/5.7/en/server-options.html#option_mysqld_transaction-isolation
[mysqld]
default-character-set = utf8
default-storage-engine = InnoDB
default-table-type = InnoDB
transaction-isolation = SERIALIZABLE
For PostgreSQL the setting is called default_transaction_isolation. You can set this in postgresql.conf.
来源:https://stackoverflow.com/questions/29583303/how-to-set-a-ruby-on-rails-4-apps-default-db-isolation-level