本文整理汇总了Python中django.db.connection.is_usable方法的典型用法代码示例。如果您正苦于以下问题:Python connection.is_usable方法的具体用法?Python connection.is_usable怎么用?Python connection.is_usable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.connection
的用法示例。
在下文中一共展示了connection.is_usable方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_database_connected
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def check_database_connected(app_configs, **kwargs):
"""
A Django check to see if connecting to the configured default
database backend succeeds.
"""
errors = []
try:
connection.ensure_connection()
except OperationalError as e:
msg = "Could not connect to database: {!s}".format(e)
errors.append(checks.Error(msg, id=health.ERROR_CANNOT_CONNECT_DATABASE))
except ImproperlyConfigured as e:
msg = 'Datbase misconfigured: "{!s}"'.format(e)
errors.append(checks.Error(msg, id=health.ERROR_MISCONFIGURED_DATABASE))
else:
if not connection.is_usable():
errors.append(
checks.Error(
"Database connection is not usable",
id=health.ERROR_UNUSABLE_DATABASE,
)
)
return errors
示例2: ensure_mysql_connection_usable
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def ensure_mysql_connection_usable():
"""Ensure that MySQL connection is usable
From: http://stackoverflow.com/questions/7835272/django-operationalerror-2006-mysql-server-has-gone-away
"""
from django.db import connection, connections
# MySQL is lazily connected to in Django.
# connection.connection is None means
# you have not connected to MySQL before
if connection.connection and not connection.is_usable():
# destroy the default MySQL connection
# after this line, when you use ORM methods
# Django will reconnect to the default MySQL
#
# Delete one database connection:
# del connections._connections.default
#
# Delete all database connections
databases = connections._connections.__dict__.keys()
for database in databases:
del connections._connections.__dict__[database]
示例3: connected
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def connected():
"""Context manager that ensures we're connected to the database.
If there is not yet a connection to the database, this will connect on
entry and disconnect on exit. Preexisting connections will be left alone.
If the preexisting connection is not usable it is closed and a new
connection is made.
"""
if connection.connection is None:
connection.close_if_unusable_or_obsolete()
connection.ensure_connection()
try:
yield
finally:
connection.close()
elif connection.is_usable():
yield
else:
# Connection is not usable, so we disconnect and reconnect. Since
# the connection was previously connected we do not disconnect this
# new connection.
connection.close_if_unusable_or_obsolete()
connection.ensure_connection()
yield
示例4: test_ensure_closed
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def test_ensure_closed(self) -> None:
from django.db import connection
connection.close()
self.assertFalse(connection.is_usable()) # should be true after setUp
示例5: test_prehook
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def test_prehook(self) -> None:
cb = Mock(name='pre_reconnect_hook')
ddr.pre_reconnect.connect(fix_connection)
ddr.pre_reconnect.connect(cb)
from django.db import connection
connection.close()
connection.s_connect = connection.connect
connection.connect = Mock(side_effect=OperationalError('reconnect testing'))
connection.ensure_connection()
ReconnectTests.cls_atomics['default'] = transaction.atomic(using='default')
ReconnectTests.cls_atomics['default'].__enter__()
self.assertTrue(cb.called)
self.assertTrue(connection.is_usable())
示例6: test_posthook
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def test_posthook(self) -> None:
cb = Mock(name='post_reconnect_hook')
ddr.pre_reconnect.connect(fix_connection)
ddr.post_reconnect.connect(cb)
from django.db import connection
connection.close()
connection.s_connect = connection.connect
connection.connect = Mock(side_effect=OperationalError('reconnect testing'))
connection.ensure_connection()
ReconnectTests.cls_atomics['default'] = transaction.atomic(using='default')
ReconnectTests.cls_atomics['default'].__enter__()
self.assertTrue(cb.called)
self.assertTrue(connection.is_usable())
示例7: _should_check_constraints
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def _should_check_constraints(self, connection):
return (
connection.features.can_defer_constraint_checks and
not connection.needs_rollback and connection.is_usable()
)
示例8: check_and_send_restart_signal
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def check_and_send_restart_signal() -> None:
try:
if not connection.is_usable():
logging.warning("*** Sending self SIGUSR1 to trigger a restart.")
os.kill(os.getpid(), signal.SIGUSR1)
except Exception:
pass
示例9: execute_callback
# 需要导入模块: from django.db import connection [as 别名]
# 或者: from django.db.connection import is_usable [as 别名]
def execute_callback(task):
"""异步任务的回调, 将结果填入数据库等等
使用django-q的hook, 传入参数为整个task
task.result 是真正的结果
"""
# https://stackoverflow.com/questions/7835272/django-operationalerror-2006-mysql-server-has-gone-away
if connection.connection and not connection.is_usable():
close_old_connections()
workflow_id = task.args[0]
# 判断工单状态,如果不是执行中的,不允许更新信息,直接抛错记录日志
with transaction.atomic():
workflow = SqlWorkflow.objects.get(id=workflow_id)
if workflow.status != 'workflow_executing':
raise Exception(f'工单{workflow.id}状态不正确,禁止重复更新执行结果!')
workflow.finish_time = task.stopped
if not task.success:
# 不成功会返回错误堆栈信息,构造一个错误信息
workflow.status = 'workflow_exception'
execute_result = ReviewSet(full_sql=workflow.sqlworkflowcontent.sql_content)
execute_result.rows = [ReviewResult(
stage='Execute failed',
errlevel=2,
stagestatus='异常终止',
errormessage=task.result,
sql=workflow.sqlworkflowcontent.sql_content)]
elif task.result.warning or task.result.error:
execute_result = task.result
workflow.status = 'workflow_exception'
else:
execute_result = task.result
workflow.status = 'workflow_finish'
# 保存执行结果
workflow.sqlworkflowcontent.execute_result = execute_result.json()
workflow.sqlworkflowcontent.save()
workflow.save()
# 增加工单日志
audit_id = Audit.detail_by_workflow_id(workflow_id=workflow_id,
workflow_type=WorkflowDict.workflow_type['sqlreview']).audit_id
Audit.add_log(audit_id=audit_id,
operation_type=6,
operation_type_desc='执行结束',
operation_info='执行结果:{}'.format(workflow.get_status_display()),
operator='',
operator_display='系统'
)
# DDL工单结束后清空实例资源缓存
if workflow.syntax_type == 1:
r = get_redis_connection("default")
for key in r.scan_iter(match='*insRes*', count=2000):
r.delete(key)
# 发送消息
notify_for_execute(workflow)