Esempio n. 1
0
    def _trace_ids(self, *args, **kwargs):
        """Bulk deletion with ascending order by id

        Ordered by ids UPDATE and DELETE are not supported by SQL standard.
        As workaround we just lock records before bulk deletion or updating
        and add trace selected ids in the appropriate deadlock_detector.Lock
        """

        # Only one table is used for DELETE and UPDATE query
        table = next(self._get_tables())
        columns = ['id']
        if self.whereclause is not None:
            # Creating ordered by id select query with whereclause from
            # origin query
            query_select = sql.expression.select(
                columns=columns, whereclause=self.whereclause)
        else:
            query_select = sql.expression.select(
                columns=columns, from_obj=table)

        query_select = query_select.order_by('id').with_for_update()
        result = db().execute(query_select)

        # We should trace locked ids only when deadlock detection activated
        if settings.DEVELOPMENT:
            dd.handle_lock(
                table, [row[0] for row in result.fetchall()])
Esempio n. 2
0
    def _trace_ids(self, *args, **kwargs):
        """Bulk deletion with ascending order by id

        Ordered by ids UPDATE and DELETE are not supported by SQL standard.
        As workaround we just lock records before bulk deletion or updating
        and add trace selected ids in the appropriate deadlock_detector.Lock
        """

        # Only one table is used for DELETE and UPDATE query
        table = next(self._get_tables())
        columns = ['id']
        if self.whereclause is not None:
            # Creating ordered by id select query with whereclause from
            # origin query
            query_select = sql.expression.select(columns=columns,
                                                 whereclause=self.whereclause)
        else:
            query_select = sql.expression.select(columns=columns,
                                                 from_obj=table)

        query_select = query_select.order_by('id').with_for_update()
        result = db().execute(query_select)

        # We should trace locked ids only when deadlock detection activated
        if settings.DEVELOPMENT:
            dd.handle_lock(table, [row[0] for row in result.fetchall()])
Esempio n. 3
0
 def __setattr__(self, key, value):
     super(ObserverModelBase, self).__setattr__(key, value)
     state = object_state(self)
     # If object exists in the DB (have id) and attached to
     # the SqlAlchemy session (state.session_id is not None)
     # UPDATE query will be generated on SqlAlchemy session
     # flush or commit. Thus we should trace such DB object
     # modifications.
     if self.id is not None and state.session_id is not None:
         dd.handle_lock(self.__tablename__, ids=(self.id, ))
Esempio n. 4
0
 def __setattr__(self, key, value):
     super(ObserverModelBase, self).__setattr__(key, value)
     state = object_state(self)
     # If object exists in the DB (have id) and attached to
     # the SqlAlchemy session (state.session_id is not None)
     # UPDATE query will be generated on SqlAlchemy session
     # flush or commit. Thus we should trace such DB object
     # modifications.
     if self.id is not None and state.session_id is not None:
         dd.handle_lock(self.__tablename__, ids=(self.id,))
Esempio n. 5
0
 def delete(self, instance):
     super(DeadlockDetectingSession, self).delete(instance)
     dd.handle_lock(instance.__tablename__, ids=(instance.id, ))
Esempio n. 6
0
 def delete(self, instance):
     super(DeadlockDetectingSession, self).delete(instance)
     dd.handle_lock(instance.__tablename__,
                    ids=(instance.id,))
Esempio n. 7
0
 def with_lockmode(self, mode):
     """with_lockmode function wrapper for deadlock detection
     """
     for ent in self._entities:
         handle_lock('{0}'.format(ent.selectable))
     return super(NoCacheQuery, self).with_lockmode(mode)
 def with_lockmode(self, mode):
     """with_lockmode function wrapper for deadlock detection
     """
     for ent in self._entities:
         handle_lock('{0}'.format(ent.selectable))
     return super(NoCacheQuery, self).with_lockmode(mode)