mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-25 14:50:24 +00:00
perf: db connection close if needt
This commit is contained in:
@@ -4,6 +4,7 @@ import os
|
||||
import sys
|
||||
|
||||
from django.apps import AppConfig
|
||||
from django.db import close_old_connections
|
||||
|
||||
|
||||
class CommonConfig(AppConfig):
|
||||
@@ -21,3 +22,4 @@ class CommonConfig(AppConfig):
|
||||
|
||||
if not os.environ.get('DJANGO_DEBUG_SHELL'):
|
||||
django_ready.send(CommonConfig)
|
||||
close_old_connections()
|
||||
|
@@ -56,7 +56,7 @@ def close_old_connections():
|
||||
|
||||
|
||||
@contextmanager
|
||||
def safe_db_connection():
|
||||
def safe_db_connection(auto_close=False):
|
||||
in_atomic_block = connection.in_atomic_block # 当前是否处于事务中
|
||||
autocommit = transaction.get_autocommit() # 是否启用了自动提交
|
||||
created = False
|
||||
@@ -69,7 +69,7 @@ def safe_db_connection():
|
||||
yield
|
||||
finally:
|
||||
# 如果不是事务中(API 请求中可能需要提交事务),则关闭连接
|
||||
if created and not in_atomic_block and autocommit:
|
||||
if auto_close or (created and not in_atomic_block and autocommit):
|
||||
print("close connection in safe_db_connection")
|
||||
close_old_connections()
|
||||
|
||||
|
Reference in New Issue
Block a user