本文整理汇总了Python中django.db.transaction.get_connection方法的典型用法代码示例。如果您正苦于以下问题:Python transaction.get_connection方法的具体用法?Python transaction.get_connection怎么用?Python transaction.get_connection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.transaction
的用法示例。
在下文中一共展示了transaction.get_connection方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _patch_atomic
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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__)
示例2: update_object
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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
)
示例3: remove_object
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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
)
示例4: update_object
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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)
示例5: remove_object
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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)
示例6: __enter__
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
def __enter__(self):
"""Enter context manager."""
connection = transaction.get_connection()
connection.set_schema(self.schema)
return self
示例7: __exit__
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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()
示例8: database_event
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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))
示例9: transaction_committed
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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()
示例10: transaction_committed
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import get_connection [as 别名]
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()