-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
70 lines (55 loc) · 1.77 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# DJANGO_SETTINGS_MODULE = 'app.settings'
# from django.middleware.transaction import TransactionMiddleware
from django.conf import settings
from django.db import transaction
settings.configure()
settings.DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '', # noqa
'USER': 'root',
'PASSWORD': '',
# 'HOST': 'localhost',
'HOST': '192.168.101.100',
'PORT': '3306',
},
# 'default': { # sqlite
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
}
def foo():
print('foo')
raise Exception('foo raise')
pass
def bar():
print('bar')
pass
try:
with transaction.atomic(): # Outer atomic, start a new transaction
transaction.on_commit(foo)
try:
with transaction.atomic(): # Inner atomic block, create a savepoint
transaction.on_commit(bar)
# Raising an exception - abort the savepoint
# raise Exception('Inner raise')
except Exception as e:
print('Inner catch:', e)
# raise Exception('Outer raise')
except Exception as e:
print('Outer catch:', e)
# @transaction.atomic
# def main():
# foo.save()
# # transaction now contains a.save()
# sid = transaction.savepoint()
# bar.save()
# # transaction now contains a.save() and b.save()
# if want_to_keep_b:
# transaction.savepoint_commit(sid)
# # open transaction still contains a.save() and b.save()
# else:
# transaction.savepoint_rollback(sid)
# # open transaction now contains only a.save()
# if __name__ == '__main__':
# main()