Django transaction isolation level in mysql & postgresql

后端 未结 5 911
野趣味
野趣味 2020-12-19 16:44

Do you know the default isolation level of the transactions used in Django? Is it possible to set the isolation level in the database independent way?

I\'m mainly in

相关标签:
5条回答
  • 2020-12-19 16:56

    (Sorry, i can't comment for Danhip) This solution wotked for me (mySQL), I added Peter's code in the DATABASE field:

    DATABASES = {
        'default': {
            (...)
            'OPTIONS': {
                (...),
                "init_command": "SET storage_engine=INNODB, SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED"
            },
         }
    }
    
    0 讨论(0)
  • 2020-12-19 16:59

    At the moment django does not set the isolation level. This is a bug, because of django does not work properly with any higher isolation level than READ COMMITTED. But MySQL is using REPEATABLE READ by default. There are plans to add a setting to set the isolation level (#14026) and a discussion about making READ COMMITTED the default (#13906). I have also written a detailed article about MySQL transactions and django.

    0 讨论(0)
  • 2020-12-19 17:07

    You can also change this per client / session using the django database options, like this:

    DATABASE_OPTIONS = { "init_command": "SET storage_engine=INNODB, SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED", }
    
    0 讨论(0)
  • 2020-12-19 17:13

    if you want to use read uncommited isolation level.

    you add the 'isolation_level': 'read uncommitted' in 'OPTIONS on database connection configuration.

    DATABASES = {
        'read-uncommited': {
            'OPTIONS': {
                'isolation_level': 'read uncommitted'
            },
        },
    }

    you can find from https://docs.djangoproject.com/en/2.1/ref/databases/

    0 讨论(0)
  • 2020-12-19 17:17

    The isolation level isn't changed by mysql drivers so it depends on the server's default isolation level.

    0 讨论(0)
提交回复
热议问题