add hack to force database connection to stay alive by hooking into DatabaseWrapper.ensure_connection, resolves #121
This commit is contained in:
parent
d6e81c6af7
commit
326cefbec1
|
@ -0,0 +1,22 @@
|
|||
import importlib
|
||||
from django.conf import settings
|
||||
from django.db.backends.utils import CursorWrapper
|
||||
|
||||
|
||||
def patch_ensure_connection():
|
||||
for name, config in settings.DATABASES.items():
|
||||
module = importlib.import_module(config['ENGINE'] + '.base')
|
||||
|
||||
def ensure_connection(self):
|
||||
if self.connection is not None:
|
||||
try:
|
||||
with CursorWrapper(self.create_cursor(), self) as cursor:
|
||||
cursor.execute('SELECT 1;')
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
with self.wrap_database_errors:
|
||||
self.connect()
|
||||
|
||||
module.DatabaseWrapper.ensure_connection = ensure_connection
|
|
@ -171,3 +171,7 @@ except ImportError as e:
|
|||
import sys
|
||||
sys.stderr.write(f'Unable to import local_settings: {e}\n')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
from .dbutils import patch_ensure_connection
|
||||
patch_ensure_connection()
|
||||
|
|
Loading…
Reference in New Issue