Django's ORM transaction and locks

ORM matters:

Transaction:

Transaction database (abbreviation: transaction) during the execution of a logical unit is a database management system, is constituted by a finite sequence of database operations.

Transaction features:

Not arbitrary sequence of operation of the database is a database transaction. Database transaction has the following four characteristics, known as customary ACID properties.

1.     Atomicity ( Atomicity ): the transaction is performed as a whole, contained therein to the database either all operations are performed or not performed.

2.     Consistency ( Consistency ): Services should ensure that the state of the database from one consistent state to another consistent state. Meaning consistent state of data in the database should meet the integrity constraints.

3.     Isolation ( Isolation ): concurrent execution of multiple transactions, executing a transaction should not affect the execution of other transactions.

4.     Persistent ( Durability Rev ): transaction has been submitted to modify the database should be permanently stored in the database.

Transaction opened in three ways:

Global open (middleware):

. Settings Py file configuration:

DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.sqlite3',

        'NAME': 'day062',

        'USER': 'root',

        'PASSWORD': '',

        'HOST': '127.0.0.1',

        'PORT': 3306,

        # 'OPTIONS': {

        #     'init_command': "SET default_storage_engine='INNODB'",

        # 'Init_command': "SET sql_mode = 'STRICT_TRANS_TABLES'",} # set the strict mode database

 

        'ATOMIC_REQUESTS' : True # Enable global affairs, it is bound to a http request corresponding whole process

        # "AUTOCOMMIT": False, # global cancel automatic submission, caution

 

        # 'Other': {# configure other databases

        #     'ENGINE': 'django.db.backends.mysql',

        #

        # }

    }

}

 

Enable global transaction is any way a http request all the corresponding sql all in one transaction execution (either all succeed, or all else fails). It is global configuration, if you want to a http request to turn on the water (and then customize the transaction), you can use non_atomic_requests decorator, then he can not control the transaction.

 

from django.db import transaction

# Enable global affairs, the abolition of certain transactions:

@transaction.non_atomic_requests

def query(request):

    # .... orm related operations

    return  the HttpResponse ( ' global transaction open, the function specified in this view cancel the transaction ' )

 

But Django documentation says, is not recommended. Because if the transaction with HTTP binding request to together, but view is dependent on the efficiency of application and database queries to the database lock contention current situation. When the flow rate up, performance will be affected.

 

 

Local open transaction (two ways):

The first: the decorator @ transcation.atomic decorative function partial view turned transactions

from django.db import transaction

@transaction.atomic

def insert(request):

    # ... orm related operations

    return the HttpResponse ( ' decorator partially open transaction ' )

 

 

The second: context management with trancation.atomic () : ... targeted local affairs open

from django.db import transaction

def delete(request):

    #...

    with transaction.atomic():

        # ... orm related operations

    #...

    return  the HttpResponse ( ' using local context open transaction ' )

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11222353.html