2

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?

Community
  • 1
  • 1
nruth
  • 1,068
  • 7
  • 22
  • 1
    Does this answer your question? [How do I set the isolation level of all my rails transactions](https://stackoverflow.com/questions/34431219/how-do-i-set-the-isolation-level-of-all-my-rails-transactions) – akostadinov Aug 12 '23 at 09:34

1 Answers1

0

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.

phylae
  • 973
  • 1
  • 10
  • 18
  • Can that be set on the database rather than the whole DBMS? It doesn't fit our use-case as we're on a hosted service without access to config files but interesting nevertheless. – nruth Oct 25 '16 at 08:37
  • For MySQL you can use `SET TRANSACTION`. You could try `SET GLOBAL TRANSACTION`, but I'm not sure if that will work if you have only restricted database access. However, you could also call `SET SESSION TRANSACTION` as the very first SQL statement, that should do the trick. I'm not totally sure how, but you could probably hook into something that runs after Rails creates a new database connection. – phylae Oct 26 '16 at 01:11