Revision 222a4f6a

b/snf-astakos-app/astakos/im/models.py
74 74
from astakos.im import auth_providers as auth
75 75

  
76 76
import astakos.im.messages as astakos_messages
77
from synnefo.lib.db.managers import ForUpdateManager
77
from snf_django.lib.db.managers import ForUpdateManager
78 78
from synnefo.lib.ordereddict import OrderedDict
79 79

  
80 80
from synnefo.lib.db.intdecimalfield import intDecimalField
b/snf-astakos-app/astakos/quotaholder/models.py
37 37
from django.db.models import (Model, BigIntegerField, CharField,
38 38
                              ForeignKey, AutoField)
39 39
from django.db import transaction
40
from synnefo.lib.db.managers import ForUpdateManager
40
from snf_django.lib.db.managers import ForUpdateManager
41 41

  
42 42
class Holding(Model):
43 43

  
/dev/null
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
from django.db import connections
31
from django.db.models import Manager
32
from django.db.models.query import QuerySet, EmptyQuerySet
33
from django.db.models.sql.datastructures import EmptyResultSet
34

  
35
class ForUpdateManager(Manager):
36
    """ Model manager implementing SELECT .. FOR UPDATE statement
37

  
38
        This manager implements select_for_update() method in order to use
39
        row-level locking in the database and guarantee exclusive access, since
40
        this method is only implemented in Django>=1.4.
41

  
42
        Non-blocking reads are not implemented, and each query including a row
43
        that is locked by another transaction will block until the lock is
44
        released. Also care must be taken in order to avoid deadlocks or retry
45
        transactions that abort due to deadlocks.
46

  
47
        Example:
48
            networks = Network.objects.filter(public=True).select_for_update()
49

  
50
    """
51

  
52
    def get_query_set(self):
53
        return ForUpdateQuerySet(self.model, using=self._db)
54

  
55
    def get_for_update(self, *args, **kwargs):
56
        query = for_update(self.filter(*args, **kwargs))
57
        query = list(query)
58
        num = len(query)
59
        if num == 1:
60
            return query[0]
61
        if not num:
62
            raise self.model.DoesNotExist(
63
                    "%s matching query does not exist. "
64
                    "Lookup parameters were %s" %
65
                    (self.model._meta.object_name, kwargs))
66
        raise self.model.MultipleObjectsReturned(
67
            "get() returned more than one %s -- it returned %s! "
68
            "Lookup parameters were %s" %
69
            (self.model._meta.object_name, num, kwargs))
70

  
71

  
72
class ForUpdateQuerySet(QuerySet):
73

  
74
    def select_for_update(self):
75
        return for_update(self)
76

  
77

  
78
def for_update(query):
79
    """ Rewrite query using SELECT .. FOR UPDATE.
80

  
81
    """
82
    if 'sqlite' in connections[query.db].settings_dict['ENGINE'].lower():
83
        # SQLite  does not support FOR UPDATE
84
        return query
85
    try:
86
        sql, params = query.query.get_compiler(query.db).as_sql()
87
    except EmptyResultSet:
88
        return EmptyQuerySet()
89
    return query.model._default_manager.raw(sql.rstrip() + ' FOR UPDATE',
90
                                            params)
b/snf-django-lib/snf_django/lib/db/managers.py
1
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29

  
30
from django.db import connections
31
from django.db.models import Manager
32
from django.db.models.query import QuerySet, EmptyQuerySet
33
from django.db.models.sql.datastructures import EmptyResultSet
34

  
35
class ForUpdateManager(Manager):
36
    """ Model manager implementing SELECT .. FOR UPDATE statement
37

  
38
        This manager implements select_for_update() method in order to use
39
        row-level locking in the database and guarantee exclusive access, since
40
        this method is only implemented in Django>=1.4.
41

  
42
        Non-blocking reads are not implemented, and each query including a row
43
        that is locked by another transaction will block until the lock is
44
        released. Also care must be taken in order to avoid deadlocks or retry
45
        transactions that abort due to deadlocks.
46

  
47
        Example:
48
            networks = Network.objects.filter(public=True).select_for_update()
49

  
50
    """
51

  
52
    def get_query_set(self):
53
        return ForUpdateQuerySet(self.model, using=self._db)
54

  
55
    def get_for_update(self, *args, **kwargs):
56
        query = for_update(self.filter(*args, **kwargs))
57
        query = list(query)
58
        num = len(query)
59
        if num == 1:
60
            return query[0]
61
        if not num:
62
            raise self.model.DoesNotExist(
63
                    "%s matching query does not exist. "
64
                    "Lookup parameters were %s" %
65
                    (self.model._meta.object_name, kwargs))
66
        raise self.model.MultipleObjectsReturned(
67
            "get() returned more than one %s -- it returned %s! "
68
            "Lookup parameters were %s" %
69
            (self.model._meta.object_name, num, kwargs))
70

  
71

  
72
class ForUpdateQuerySet(QuerySet):
73

  
74
    def select_for_update(self):
75
        return for_update(self)
76

  
77

  
78
def for_update(query):
79
    """ Rewrite query using SELECT .. FOR UPDATE.
80

  
81
    """
82
    if 'sqlite' in connections[query.db].settings_dict['ENGINE'].lower():
83
        # SQLite  does not support FOR UPDATE
84
        return query
85
    try:
86
        sql, params = query.query.get_compiler(query.db).as_sql()
87
    except EmptyResultSet:
88
        return EmptyQuerySet()
89
    return query.model._default_manager.raw(sql.rstrip() + ' FOR UPDATE',
90
                                            params)

Also available in: Unified diff