Example #1
0
    def run_select_for_update(self, status, nowait=False):
        """
        Utility method that runs a SELECT FOR UPDATE against all
        Person instances. After the select_for_update, it attempts
        to update the name of the only record, save, and commit.

        This function expects to run in a separate thread.
        """
        status.append('started')
        try:
            # We need to enter transaction management again, as this is done on
            # per-thread basis
            transaction.enter_transaction_management()
            people = list(
                Person.objects.all().select_for_update(nowait=nowait)
            )
            people[0].name = 'Fred'
            people[0].save()
            transaction.commit()
        except DatabaseError as e:
            status.append(e)
        finally:
            # This method is run in a separate thread. It uses its own
            # database connection. Close it without waiting for the GC.
            transaction.abort()
            connection.close()
Example #2
0
    def process(self, backend):
        self.log.debug("Checking backend for items")
        self.set_process_title("Waiting for items")

        # Tell master process that we are not processing anything.
        self.tell_master(None, False)

        job = backend.dequeue(self.queue, 1)
        if job is None:
            return

        # Update master what we are doing
        self.tell_master(
            job.timeout,
            job.sigkill_on_stop,
        )

        self.log.debug("Running job %s", job)
        self.set_process_title("Running job %s" % job)

        if job.run() and self.touch_filename:
            with open(self.touch_filename, 'a'):
                os.utime(self.touch_filename, None)

        # Emulate Django's request_finished signal and close all of our
        # connections. Django assumes that making a DB connection is cheap, so
        # it's probably safe to assume that too.
        for x in connections:
            try:
                # Removed in recent versions
                transaction.abort(x)
            except AttributeError:
                pass
            connections[x].close()
Example #3
0
    def process(self, backend):
        self.log.debug("Checking backend for items")
        self.set_process_title("Waiting for items")

        # Tell master process that we are not processing anything.
        self.tell_master(None, False)

        job = backend.dequeue(self.queue, 1)
        if job is None:
            return

        # Update master what we are doing
        self.tell_master(
            job.timeout,
            job.sigkill_on_stop,
        )

        self.log.debug("Running job %s", job)
        self.set_process_title("Running job %s" % job)

        if job.run() and self.touch_filename:
            with open(self.touch_filename, 'a'):
                os.utime(self.touch_filename, None)

        # Emulate Django's request_finished signal and close all of our
        # connections. Django assumes that making a DB connection is cheap, so
        # it's probably safe to assume that too.
        for x in connections:
            try:
                # Removed in recent versions
                transaction.abort(x)
            except AttributeError:
                pass
            connections[x].close()
Example #4
0
def close_connection(**kwargs):
    # Avoid circular imports
    from django.db import transaction
    for conn in connections:
        # If an error happens here the connection will be left in broken
        # state. Once a good db connection is again available, the
        # connection state will be cleaned up.
        transaction.abort(conn)
        connections[conn].close()
Example #5
0
def close_connection(**kwargs):
    # Avoid circular imports
    from django.db import transaction
    for conn in connections:
        # If an error happens here the connection will be left in broken
        # state. Once a good db connection is again available, the
        # connection state will be cleaned up.
        transaction.abort(conn)
        connections[conn].close()
Example #6
0
def close_connection(**kwargs):
    warnings.warn("close_connection is superseded by close_old_connections.", RemovedInDjango18Warning, stacklevel=2)
    # Avoid circular imports
    from django.db import transaction

    for conn in connections:
        # If an error happens here the connection will be left in broken
        # state. Once a good db connection is again available, the
        # connection state will be cleaned up.
        transaction.abort(conn)
        connections[conn].close()
Example #7
0
def close_connection(**kwargs):
    # Avoid circular imports
    from django.db import transaction
    for conn in connections:
        try:
            transaction.abort(conn)
            connections[conn].close()
        except Exception:
            # The connection's state is unknown, so it has to be
            # abandoned. This could happen for example if the network
            # connection has a failure.
            del connections[conn]
Example #8
0
def close_connection(**kwargs):
    warnings.warn(
        "close_connection is superseded by close_old_connections.",
        RemovedInDjango18Warning, stacklevel=2)
    # Avoid circular imports
    from django.db import transaction
    for conn in connections:
        # If an error happens here the connection will be left in broken
        # state. Once a good db connection is again available, the
        # connection state will be cleaned up.
        transaction.abort(conn)
        connections[conn].close()
Example #9
0
 def tearDown(self):
     try:
         # We don't really care if this fails - some of the tests will set
         # this in the course of their run.
         transaction.abort()
         self.new_connection.abort()
     except transaction.TransactionManagementError:
         pass
     self.new_connection.close()
     try:
         self.end_blocking_transaction()
     except (DatabaseError, AttributeError):
         pass
Example #10
0
 def tearDown(self):
     try:
         # We don't really care if this fails - some of the tests will set
         # this in the course of their run.
         transaction.abort()
         self.new_connection.abort()
     except transaction.TransactionManagementError:
         pass
     self.new_connection.close()
     try:
         self.end_blocking_transaction()
     except (DatabaseError, AttributeError):
         pass
Example #11
0
    def close_connection(self, connection):
        """
            关闭数据库
        """
        alias = connection.alias
        connection_list = self.pooled_connections[alias]
        # connection_list = []
        max_conn = settings.ALIAS_2_MAX_CONNECT_NUM.get(alias, 10)

        # 清空状态
        from django.db import transaction
        transaction.abort(connection)

        # 关闭,或者归还到Pool中
        connection.close()
        connection.connection_handler = None

        if len(connection_list) > max_conn:
            pass
        else:
            connection_list.append(connection)
Example #12
0
    def close_connection(self, connection):
        """
            关闭数据库
        """
        alias = connection.alias
        connection_list = self.pooled_connections[alias]
        # connection_list = []
        max_conn = settings.ALIAS_2_MAX_CONNECT_NUM.get(alias, 10)

        # 清空状态
        from django.db import transaction
        transaction.abort(connection)

        # 关闭,或者归还到Pool中
        connection.close()
        connection.connection_handler = None

        if len(connection_list) > max_conn:
            pass
        else:
            connection_list.append(connection)
Example #13
0
    def _get_content(self):
        transaction.enter_transaction_management()
        # transaction.managed(False)
        # sid = transaction.savepoint()
        self._assets = defaultdict(list)
        try:
            output = self.callable
            while callable(output):

                # collect css/js in an ordered fashion, de-duping on the way.
                if hasattr(output, 'assets'):
                    for key, value in output.assets.items():
                        for thing in value:
                            if thing not in self._assets[key]:
                                self._assets[key].append(thing)

                # it's probably a class instance, or a response generator
                output = output(request=self.request)
        finally:
            transaction.abort()
            # transaction.savepoint_rollback(sid)
            # transaction.leave_transaction_management()

        return "\n".join(x for x in output.splitlines() if x.strip())
Example #14
0
 def tearDown(self):
     transaction.abort()
Example #15
0
 def tearDown(self):
     transaction.abort()
     super(TransactionMiddlewareTest, self).tearDown()
Example #16
0
 def tearDown(self):
     transaction.abort()
     super(TransactionMiddlewareTest, self).tearDown()