root / snf-django-lib / snf_django / lib / db / transaction.py @ 0f66865f
History | View | Annotate | Download (2 kB)
1 | 85c46d85 | Giorgos Korfiatis | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 85c46d85 | Giorgos Korfiatis | #
|
3 | 85c46d85 | Giorgos Korfiatis | # Redistribution and use in source and binary forms, with or
|
4 | 85c46d85 | Giorgos Korfiatis | # without modification, are permitted provided that the following
|
5 | 85c46d85 | Giorgos Korfiatis | # conditions are met:
|
6 | 85c46d85 | Giorgos Korfiatis | #
|
7 | 85c46d85 | Giorgos Korfiatis | # 1. Redistributions of source code must retain the above
|
8 | 85c46d85 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
9 | 85c46d85 | Giorgos Korfiatis | # disclaimer.
|
10 | 85c46d85 | Giorgos Korfiatis | #
|
11 | 85c46d85 | Giorgos Korfiatis | # 2. Redistributions in binary form must reproduce the above
|
12 | 85c46d85 | Giorgos Korfiatis | # copyright notice, this list of conditions and the following
|
13 | 85c46d85 | Giorgos Korfiatis | # disclaimer in the documentation and/or other materials
|
14 | 85c46d85 | Giorgos Korfiatis | # provided with the distribution.
|
15 | 85c46d85 | Giorgos Korfiatis | #
|
16 | 85c46d85 | Giorgos Korfiatis | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 85c46d85 | Giorgos Korfiatis | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 85c46d85 | Giorgos Korfiatis | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 85c46d85 | Giorgos Korfiatis | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 85c46d85 | Giorgos Korfiatis | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 85c46d85 | Giorgos Korfiatis | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 85c46d85 | Giorgos Korfiatis | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 85c46d85 | Giorgos Korfiatis | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 85c46d85 | Giorgos Korfiatis | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 85c46d85 | Giorgos Korfiatis | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 85c46d85 | Giorgos Korfiatis | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 85c46d85 | Giorgos Korfiatis | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 85c46d85 | Giorgos Korfiatis | #
|
29 | 85c46d85 | Giorgos Korfiatis | # The views and conclusions contained in the software and
|
30 | 85c46d85 | Giorgos Korfiatis | # documentation are those of the authors and should not be
|
31 | 85c46d85 | Giorgos Korfiatis | # interpreted as representing official policies, either expressed
|
32 | 85c46d85 | Giorgos Korfiatis | # or implied, of GRNET S.A.
|
33 | 85c46d85 | Giorgos Korfiatis | |
34 | 85c46d85 | Giorgos Korfiatis | |
35 | 0148b2cb | Giorgos Korfiatis | from functools import wraps |
36 | 39b2cb50 | Giorgos Korfiatis | from django.db import transaction |
37 | 85c46d85 | Giorgos Korfiatis | |
38 | 85c46d85 | Giorgos Korfiatis | |
39 | 39b2cb50 | Giorgos Korfiatis | def commit_on_success_strict(**kwargs): |
40 | 39b2cb50 | Giorgos Korfiatis | def wrap(func): |
41 | 0148b2cb | Giorgos Korfiatis | @wraps(func)
|
42 | 39b2cb50 | Giorgos Korfiatis | @transaction.commit_manually(**kwargs)
|
43 | 39b2cb50 | Giorgos Korfiatis | def inner(*args, **kwargs): |
44 | 39b2cb50 | Giorgos Korfiatis | try:
|
45 | 39b2cb50 | Giorgos Korfiatis | result = func(*args, **kwargs) |
46 | 39b2cb50 | Giorgos Korfiatis | transaction.commit() |
47 | 39b2cb50 | Giorgos Korfiatis | return result
|
48 | 39b2cb50 | Giorgos Korfiatis | except BaseException as e: |
49 | 39b2cb50 | Giorgos Korfiatis | transaction.rollback() |
50 | 39b2cb50 | Giorgos Korfiatis | raise
|
51 | 39b2cb50 | Giorgos Korfiatis | return inner
|
52 | 39b2cb50 | Giorgos Korfiatis | return wrap |