Python django.db.transaction.get_connection() Examples

The following are 10 code examples of django.db.transaction.get_connection(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.transaction , or try the search function .
Example #1
Source File: monkey_patch.py    From django-cachalot with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _patch_atomic():
    def patch_enter(original):
        @wraps(original)
        def inner(self):
            cachalot_caches.enter_atomic(self.using)
            original(self)

        return inner

    def patch_exit(original):
        @wraps(original)
        def inner(self, exc_type, exc_value, traceback):
            needs_rollback = get_connection(self.using).needs_rollback
            try:
                original(self, exc_type, exc_value, traceback)
            finally:
                cachalot_caches.exit_atomic(
                    self.using, exc_type is None and not needs_rollback)

        return inner

    Atomic.__enter__ = patch_enter(Atomic.__enter__)
    Atomic.__exit__ = patch_exit(Atomic.__exit__) 
Example #2
Source File: index.py    From aries-vcr with Apache License 2.0 6 votes vote down vote up
def update_object(self, instance, using=None, **kwargs):
        LOGGER.debug("Updating object; %s ...", instance.id)
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            if self._transaction_savepts != conn.savepoint_ids:
                self._transaction_savepts = conn.savepoint_ids
                conn.on_commit(self.transaction_committed)
            if self.should_update(instance, **kwargs):
                if not using:
                    using = "default"
                if using not in self._transaction_added:
                    self._transaction_added[using] = {}
                self._transaction_added[using][instance.id] = instance
        else:
            if self._transaction_added or self._transaction_removed:
                # previous transaction must have ended with rollback
                self.reset()
            if self._backend_queue:
                self._backend_queue.add(self.__class__, using, [instance])
            else:
                super(TxnAwareSearchIndex, self).update_object(
                    instance, using, **kwargs
                ) 
Example #3
Source File: index.py    From aries-vcr with Apache License 2.0 6 votes vote down vote up
def remove_object(self, instance, using=None, **kwargs):
        LOGGER.debug("Removing object; %s ...", instance.id)
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            if self._transaction_savepts != conn.savepoint_ids:
                self._transaction_savepts = conn.savepoint_ids
                conn.on_commit(self.transaction_committed)
            if not using:
                using = "default"
            if using not in self._transaction_removed:
                self._transaction_removed[using] = {}
            self._transaction_removed[using][instance.id] = instance
        else:
            if self._transaction_added or self._transaction_removed:
                # previous transaction must have ended with rollback
                self.reset()
            if self._backend_queue:
                self._backend_queue.delete(self.__class__, using, [instance])
            else:
                super(TxnAwareSearchIndex, self).remove_object(
                    instance, using, **kwargs
                ) 
Example #4
Source File: index.py    From TheOrgBook with Apache License 2.0 6 votes vote down vote up
def update_object(self, instance, using=None, **kwargs):
        LOGGER.debug("Updating object; %s ...", instance.id)
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            if self._transaction_savepts != conn.savepoint_ids:
                self._transaction_savepts = conn.savepoint_ids
                conn.on_commit(self.transaction_committed)
            if self.should_update(instance, **kwargs):
                if not using:
                    using = "default"
                if using not in self._transaction_added:
                    self._transaction_added[using] = {}
                self._transaction_added[using][instance.id] = instance
        else:
            if self._transaction_added or self._transaction_removed:
                # previous transaction must have ended with rollback
                self.reset()
            if self._backend_queue:
                self._backend_queue.add(self.__class__, using, [instance])
            else:
                super(TxnAwareSearchIndex, self).update_object(instance, using, **kwargs) 
Example #5
Source File: index.py    From TheOrgBook with Apache License 2.0 6 votes vote down vote up
def remove_object(self, instance, using=None, **kwargs):
        LOGGER.debug("Removing object; %s ...", instance.id)
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            if self._transaction_savepts != conn.savepoint_ids:
                self._transaction_savepts = conn.savepoint_ids
                conn.on_commit(self.transaction_committed)
            if not using:
                using = "default"
            if using not in self._transaction_removed:
                self._transaction_removed[using] = {}
            self._transaction_removed[using][instance.id] = instance
        else:
            if self._transaction_added or self._transaction_removed:
                # previous transaction must have ended with rollback
                self.reset()
            if self._backend_queue:
                self._backend_queue.delete(self.__class__, using, [instance])
            else:
                super(TxnAwareSearchIndex, self).remove_object(instance, using, **kwargs) 
Example #6
Source File: koku_database_access.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def __enter__(self):
        """Enter context manager."""
        connection = transaction.get_connection()
        connection.set_schema(self.schema)
        return self 
Example #7
Source File: koku_database_access.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def __exit__(self, exception_type, exception_value, traceback):
        """Context manager reset schema to public and exit."""
        connection = transaction.get_connection()
        connection.set_schema_to_public() 
Example #8
Source File: model_observer.py    From djangochannelsrestframework with MIT License 5 votes vote down vote up
def database_event(self, instance: Model, action: Action):

        connection = transaction.get_connection()

        if connection.in_atomic_block:
            if len(connection.savepoint_ids) > 0:
                warnings.warn(
                    "Model observation with save points is unsupported and will"
                    " result in unexpected beauvoir.",
                    UnsupportedWarning,
                )

        connection.on_commit(partial(self.post_change_receiver, instance, action)) 
Example #9
Source File: index.py    From TheOrgBook with Apache License 2.0 5 votes vote down vote up
def transaction_committed(self):
        LOGGER.debug("Committing transaction(s) ...")
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            # committed nested transaction - ensure hook is attached
            self._transaction_savepts = conn.savepoint_ids
            conn.on_commit(self.transaction_committed)
        else:
            for using, instances in self._transaction_removed.items():
                if instances:
                    LOGGER.debug("Committing %d deferred Solr delete(s) after transaction.", len(instances))
                    if self._backend_queue:
                        self._backend_queue.delete(self.__class__, using, list(instances.values()))
                    else:
                        backend = self.get_backend(using)
                        if backend is not None:
                            for instance in instances.values():
                                backend.remove(instance)
                        else:
                            LOGGER.error("Failed to get backend.  Unable to commit %d deferred Solr delete(s) after transaction.", len(instances))

            for using, instances in self._transaction_added.items():
                if instances:
                    LOGGER.debug("Committing %d deferred Solr update(s) after transaction ...", len(instances))
                    if self._backend_queue:
                        self._backend_queue.add(self.__class__, using, list(instances.values()))
                    else:
                        backend = self.get_backend(using)
                        if backend is not None:
                            backend.update(self, instances.values())
                        else:
                            LOGGER.error("Failed to get backend.  Unable to commit %d deferred Solr update(s) after transaction.", len(instances))
            self.reset() 
Example #10
Source File: index.py    From aries-vcr with Apache License 2.0 4 votes vote down vote up
def transaction_committed(self):
        LOGGER.debug("Committing transaction(s) ...")
        conn = transaction.get_connection()
        if conn.in_atomic_block:
            # committed nested transaction - ensure hook is attached
            self._transaction_savepts = conn.savepoint_ids
            conn.on_commit(self.transaction_committed)
        else:
            for using, instances in self._transaction_removed.items():
                if instances:
                    LOGGER.debug(
                        "Committing %d deferred Solr delete(s) after transaction...",
                        len(instances),
                    )
                    if self._backend_queue:
                        self._backend_queue.delete(
                            self.__class__, using, list(instances.values())
                        )
                    else:
                        backend = self.get_backend(using)
                        if backend is not None:
                            for instance in instances.values():
                                backend.remove(instance)
                        else:
                            LOGGER.error(
                                "Failed to get backend.  Unable to commit %d deferred Solr delete(s) after transaction.",
                                len(instances),
                            )

            for using, instances in self._transaction_added.items():
                if instances:
                    LOGGER.debug(
                        "Committing %d deferred Solr update(s) after transaction",
                        len(instances),
                    )
                    if self._backend_queue:
                        self._backend_queue.add(
                            self.__class__, using, list(instances.values())
                        )
                    else:
                        backend = self.get_backend(using)
                        if backend is not None:
                            backend.update(self, instances.values())
                        else:
                            LOGGER.error(
                                "Failed to get backend.  Unable to commit %d deferred Solr update(s) after transaction.",
                                len(instances),
                            )
            self.reset()