Django事务未按预期工作

时间:2019-08-18 06:14:46

标签: python mysql django transactions

使用Python 3.6 / Django 1.11.2 / MySQL 5.7.19

我想通过Django框架将作业信息添加到MySQL数据库中,并确保不添加数据库中已存在的作业。而且我已经使用带有保存点的事务原子事务了,但是在测试期间仍然无法正常工作。

有人可以对如何使其起作用有任何想法吗?预先感谢!

Django settings.py

DATABASES = {
    'default': {
         'ENGINE': 'django.db.backends.mysql', 
         'NAME': 'abc',                       
         'USER': 'root',                       
         'PASSWORD': 'root',                   
         'HOST': 'localhost',                           
         'PORT': '3306',                           
         'ATOMIC_REQUEST': True,
    }
}

Django views.py:

def insert(request):
    name = request.GET["name"]
    jobs = Job.objects.filter(~Q(status='DONE'),name=name)
    if len(jobs) > 0:
        return HttpResponse("Already insert!")
    else:
            try:
                with transaction.atomic():
                    sid = transaction.savepoint()
                    newJob = Job(name=name,status='NEW')
                    newJob.save() # Could throw exception
                    jobs = Job.objects.filter(~Q(status='DONE'),name=name)
                    if len(jobs) >= 2:
                        transaction.savepoint_rollback(sid)
                        return HttpResponse("Rollback Already insert!")
                    transaction.savepoint_commit(sid)
                    return HttpResponse("insert "+ name +" successfuly")
            except IntegrityError:
                transaction.savepoint_rollback(sid)
                return HttpResponse("Rollback insert error!")
            transaction.clean_savepoints()

测试功能:

import multiprocessing
import os
import random
import time
from multiprocessing import Pool, Process

import requests


def run_proc(name):
    print("exec "+name)
    payload = {}
    payload['name'] = name
    r = requests.get("http://localhost:8080/dashboard/insert",params=payload)
    print(r.text)


if __name__ == "__main__":
    pool = multiprocessing.Pool(processes = 10)
    for j in range(10):
        for i in range(10):
            pool.apply_async(run_proc, (str(j), ))

    print("Start")
    pool.close()
    pool.join()   
    print("Sub-process(es) done.")

在数据库中:(您需要尝试几次以验证结果始终能按预期工作)

1   0   2019-08-18 06:00:39.408953  NEW
2   1   2019-08-18 06:00:41.477532  NEW
3   2   2019-08-18 06:00:43.530742  NEW
4   3   2019-08-18 06:00:45.569131  NEW
5   4   2019-08-18 06:00:47.618674  NEW
6   5   2019-08-18 06:00:49.645471  NEW
7   6   2019-08-18 06:00:51.669822  NEW
8   7   2019-08-18 06:00:53.726066  NEW
9   8   2019-08-18 06:00:55.767121  NEW
10  9   2019-08-18 06:00:57.810425  NEW
11  9   2019-08-18 06:00:57.811424  NEW >> not as expected

另一项测试:

Start
exec 0
exec 0
exec 0
exec 0
exec 0
exec 0
exec 0
exec 0
exec 0
exec 0
Already insert!
exec 1
Already insert!
exec 1
Already insert!
exec 1
insert 0 successfuly
exec 1
Already insert!
exec 1
Already insert!
exec 1
Already insert!
exec 1
Already insert!
Already insert!
exec 1
exec 1
Already insert!
exec 1
Already insert!
exec 2
Already insert!
exec 2
insert 1 successfuly
exec 2
Already insert!
exec 2
Already insert!
Already insert!
exec 2
exec 2
Rollback Already insert!
exec 2
Already insert!
exec 2
Already insert!
exec 2
Already insert!
exec 2
Already insert!
exec 3
insert 2 successfuly
exec 3
Already insert!
exec 3
Already insert!
Already insert!
Already insert!
exec 3
Already insert!
exec 3
exec 3
exec 3
Already insert!
exec 3
Already insert!
Already insert!
exec 3
exec 3
Already insert!
exec 4
Already insert!
exec 4
insert 3 successfuly
exec 4
Already insert!
Already insert!
Already insert!
exec 4
exec 4
exec 4
Already insert!
Already insert!
exec 4
exec 4
Already insert!
exec 4
Already insert!
exec 4
Already insert!
exec 5
Already insert!
exec 5
insert 4 successfuly
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 5
Already insert!
exec 6
Already insert!
exec 6
insert 5 successfuly
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 6
Already insert!
exec 7
Already insert!
exec 7
insert 6 successfuly
exec 7
Already insert!
exec 7
Already insert!
exec 7
Already insert!
exec 7
Already insert!
exec 7
Already insert!
exec 7
Already insert!
Already insert!
exec 7
exec 7
insert 7 successfuly
exec 8
Already insert!
exec 8
insert 7 successfuly
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 8
Already insert!
exec 9
Already insert!
exec 9
insert 8 successfuly
Already insert!
exec 9
exec 9
Already insert!
exec 9
Already insert!
exec 9
Already insert!
exec 9
Already insert!
exec 9
Already insert!
exec 9
Already insert!
exec 9
Already insert!
Already insert!
insert 9 successfuly
Already insert!
Already insert!
Already insert!
Already insert!
Already insert!
Already insert!
Already insert!
Sub-process(es) done.
1   0   2019-08-18 08:44:19.225353  NEW
2   1   2019-08-18 08:44:21.278121  NEW
4   2   2019-08-18 08:44:23.319939  NEW
5   3   2019-08-18 08:44:25.352651  NEW
6   4   2019-08-18 08:44:27.389727  NEW
7   5   2019-08-18 08:44:29.419754  NEW
8   6   2019-08-18 08:44:31.459066  NEW
9   7   2019-08-18 08:44:33.491298  NEW
10  7   2019-08-18 08:44:33.492298  NEW >> not expected
11  8   2019-08-18 08:44:35.534899  NEW
12  9   2019-08-18 08:44:37.558186  NEW

0 个答案:

没有答案