本文整理汇总了Python中django.db.transaction.TransactionManagementError方法的典型用法代码示例。如果您正苦于以下问题:Python transaction.TransactionManagementError方法的具体用法?Python transaction.TransactionManagementError怎么用?Python transaction.TransactionManagementError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.transaction
的用法示例。
在下文中一共展示了transaction.TransactionManagementError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: leave_transaction_management
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def leave_transaction_management(self):
"""
Leaves transaction management for a running thread. A dirty flag is carried
over to the surrounding block, as a commit will commit all changes, even
those from outside. (Commits are on connection level.)
"""
if self.transaction_state:
del self.transaction_state[-1]
else:
raise TransactionManagementError(
"This code isn't under transaction management")
# We will pass the next status (after leaving the previous state
# behind) to subclass hook.
self._leave_transaction_management(self.is_managed())
if self._dirty:
self.rollback()
raise TransactionManagementError(
"Transaction managed block ended with pending COMMIT/ROLLBACK")
self._dirty = False
示例2: managed
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def managed(self, flag=True):
"""
Puts the transaction manager into a manual state: managed transactions have
to be committed explicitly by the user. If you switch off transaction
management and there is a pending commit/rollback, the data will be
commited.
"""
top = self.transaction_state
if top:
top[-1] = flag
if not flag and self.is_dirty():
self._commit()
self.set_clean()
else:
raise TransactionManagementError("This code isn't under transaction "
"management")
示例3: acquire
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def acquire(self):
connection = connections[self.db]
qn = connection.ops.quote_name
with connection.cursor() as cursor:
if not connection.get_autocommit():
raise TransactionManagementError(
"InnoDB requires that we not be in a transaction when "
"gaining a table lock."
)
# Begin transaction - does 'SET autocommit = 0'
self._atomic = atomic(using=self.db)
self._atomic.__enter__()
locks = []
for name in self.read:
locks.append("{} READ".format(qn(name)))
for name in self.write:
locks.append("{} WRITE".format(qn(name)))
cursor.execute("LOCK TABLES {}".format(", ".join(locks)))
示例4: savepoint
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def savepoint():
"""Context manager to wrap the code within a savepoint.
This also enters a savepoint context for post-commit hooks, and so should
always be used in preference to `transaction.atomic()` when only a
savepoint is needed.
If either a transaction or a savepoint within a transaction is what you
want, use the `transactional` decorator.
If you want a _decorator_ specifically, use the `transactional` decorator.
If you want a _savepoint decorator_ specifically, write one, or adapt
this to do it.
"""
if connection.in_atomic_block:
with post_commit_hooks.savepoint():
with transaction.atomic():
yield
else:
raise TransactionManagementError(
"Savepoints cannot be created outside of a transaction."
)
示例5: validate_in_transaction
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def validate_in_transaction(connection):
"""Ensure that `connection` is within a transaction.
This only enquires as to Django's perspective on the situation. It does
not actually check that the database agrees with Django.
:raise TransactionManagementError: If no transaction is in progress.
"""
if not in_transaction(connection):
raise TransactionManagementError(
# XXX: GavinPanella 2015-08-07 bug=1482563: This error message is
# specific to lobjects, but this lives in a general utils module.
"PostgreSQL's large object support demands that all interactions "
"are done in a transaction. Further, lobject() has been known to "
"segfault when used outside of a transaction. This assertion has "
"prevented the use of lobject() outside of a transaction. Please "
"investigate."
)
示例6: get_rollback
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def get_rollback(self):
"""
Get the "needs rollback" flag -- for *advanced use* only.
"""
if not self.in_atomic_block:
raise TransactionManagementError(
"The rollback flag doesn't work outside of an 'atomic' block.")
return self.needs_rollback
示例7: set_rollback
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def set_rollback(self, rollback):
"""
Set or unset the "needs rollback" flag -- for *advanced use* only.
"""
if not self.in_atomic_block:
raise TransactionManagementError(
"The rollback flag doesn't work outside of an 'atomic' block.")
self.needs_rollback = rollback
示例8: validate_no_atomic_block
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def validate_no_atomic_block(self):
"""
Raise an error if an atomic block is active.
"""
if self.in_atomic_block:
raise TransactionManagementError(
"This is forbidden when an 'atomic' block is active.")
示例9: validate_no_broken_transaction
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def validate_no_broken_transaction(self):
if self.needs_rollback:
raise TransactionManagementError(
"An error occurred in the current transaction. You can't "
"execute queries until the end of the 'atomic' block.")
# ##### Foreign key constraints checks handling #####
示例10: test_select_for_update
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def test_select_for_update(self):
"""
Tests if ``select_for_update`` queries are not cached.
"""
with self.assertRaises(TransactionManagementError):
list(Test.objects.select_for_update())
with self.assertNumQueries(1):
with transaction.atomic():
data1 = list(Test.objects.select_for_update())
self.assertListEqual(data1, [self.t1, self.t2])
self.assertListEqual([t.name for t in data1],
['test1', 'test2'])
with self.assertNumQueries(1):
with transaction.atomic():
data2 = list(Test.objects.select_for_update())
self.assertListEqual(data2, [self.t1, self.t2])
self.assertListEqual([t.name for t in data2],
['test1', 'test2'])
with self.assertNumQueries(2):
with transaction.atomic():
data3 = list(Test.objects.select_for_update())
data4 = list(Test.objects.select_for_update())
self.assertListEqual(data3, [self.t1, self.t2])
self.assertListEqual(data4, [self.t1, self.t2])
self.assertListEqual([t.name for t in data3],
['test1', 'test2'])
self.assertListEqual([t.name for t in data4],
['test1', 'test2'])
示例11: get_rollback
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def get_rollback(self):
"""Get the "needs rollback" flag -- for *advanced use* only."""
if not self.in_atomic_block:
raise TransactionManagementError(
"The rollback flag doesn't work outside of an 'atomic' block.")
return self.needs_rollback
示例12: validate_no_atomic_block
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def validate_no_atomic_block(self):
"""Raise an error if an atomic block is active."""
if self.in_atomic_block:
raise TransactionManagementError(
"This is forbidden when an 'atomic' block is active.")
示例13: on_commit
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def on_commit(self, func):
if self.in_atomic_block:
# Transaction in progress; save for execution on commit.
self.run_on_commit.append((set(self.savepoint_ids), func))
elif not self.get_autocommit():
raise TransactionManagementError('on_commit() cannot be used in manual transaction management')
else:
# No transaction in progress and in autocommit mode; execute
# immediately.
func()
示例14: run_migrations
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def run_migrations(args, options, executor_codename, schema_name, allow_atomic=True, idx=None, count=None):
from django.core.management import color
from django.core.management.base import OutputWrapper
from django.db import connections
style = color.color_style()
def style_func(msg):
percent_str = ''
if idx is not None and count is not None and count > 0:
percent_str = '%d/%d (%s%%) ' % (idx + 1, count, int(100 * (idx + 1) / count))
return '[%s%s:%s] %s' % (
percent_str,
style.NOTICE(executor_codename),
style.NOTICE(schema_name),
msg
)
connection = connections[options.get('database', get_tenant_database_alias())]
connection.set_schema(schema_name)
stdout = OutputWrapper(sys.stdout)
stdout.style_func = style_func
stderr = OutputWrapper(sys.stderr)
stderr.style_func = style_func
if int(options.get('verbosity', 1)) >= 1:
stdout.write(style.NOTICE("=== Starting migration"))
MigrateCommand(stdout=stdout, stderr=stderr).execute(*args, **options)
try:
transaction.commit()
connection.close()
connection.connection = None
except transaction.TransactionManagementError:
if not allow_atomic:
raise
# We are in atomic transaction, don't close connections
pass
connection.set_schema_to_public()
示例15: test_tenant_schema_is_created_atomically
# 需要导入模块: from django.db import transaction [as 别名]
# 或者: from django.db.transaction import TransactionManagementError [as 别名]
def test_tenant_schema_is_created_atomically(self):
"""
When saving a tenant, it's schema should be created.
This should work in atomic transactions too.
"""
executor = get_executor()
Tenant = get_tenant_model()
schema_name = 'test'
@transaction.atomic()
def atomically_create_tenant():
t = Tenant(schema_name=schema_name)
t.save()
self.created = [t]
if executor == 'simple':
atomically_create_tenant()
self.assertTrue(schema_exists(schema_name))
elif executor == 'multiprocessing':
# Unfortunately, it's impossible for the multiprocessing executor
# to assert atomic transactions when creating a tenant
with self.assertRaises(transaction.TransactionManagementError):
atomically_create_tenant()