mirror of https://github.com/jumpserver/jumpserver
fix: merge_delay_run 偶尔会出现 (2006, MySQL server has gone away 的报错)
parent
c0273dc698
commit
660572a0ea
|
@ -1,6 +1,6 @@
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from django.db import connections
|
from django.db import connections, transaction
|
||||||
from django.utils.encoding import force_str
|
from django.utils.encoding import force_str
|
||||||
|
|
||||||
from common.utils import get_logger, signer, crypto
|
from common.utils import get_logger, signer, crypto
|
||||||
|
@ -58,6 +58,17 @@ def safe_db_connection():
|
||||||
close_old_connections()
|
close_old_connections()
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def open_db_connection(alias='default'):
|
||||||
|
connection = transaction.get_connection(alias)
|
||||||
|
try:
|
||||||
|
connection.connect()
|
||||||
|
with transaction.atomic():
|
||||||
|
yield connection
|
||||||
|
finally:
|
||||||
|
connection.close()
|
||||||
|
|
||||||
|
|
||||||
class Encryptor:
|
class Encryptor:
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.value = force_str(value)
|
self.value = force_str(value)
|
||||||
|
|
|
@ -12,6 +12,7 @@ from functools import wraps
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
|
|
||||||
from .utils import logger
|
from .utils import logger
|
||||||
|
from .db.utils import open_db_connection
|
||||||
|
|
||||||
|
|
||||||
def on_transaction_commit(func):
|
def on_transaction_commit(func):
|
||||||
|
@ -146,7 +147,9 @@ ignore_err_exceptions = (
|
||||||
def _run_func_with_org(key, org, func, *args, **kwargs):
|
def _run_func_with_org(key, org, func, *args, **kwargs):
|
||||||
from orgs.utils import set_current_org
|
from orgs.utils import set_current_org
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
with open_db_connection() as conn:
|
||||||
|
# 保证执行时使用的是新的 connection 数据库连接
|
||||||
|
# 避免出现 MySQL server has gone away 的情况
|
||||||
set_current_org(org)
|
set_current_org(org)
|
||||||
func(*args, **kwargs)
|
func(*args, **kwargs)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in New Issue