


Solving the problem of Django database connection loss (explanation with examples)
The content of this article is about solving the problem of Django database connection loss (explanation with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Problem
When using mysql in Django, the database connection may occasionally be lost. The errors usually include the following two types
1. `OperationalError: (2006, 'MySQL server has gone away')` 1. `OperationalError: (2013, 'Lost connection to MySQL server during query')`
Query mysql global variables SHOW GLOBAL VARIABLES ;You can see wait_timeout, this variable represents the connection idle time. If the client uses one connection to query the database multiple times, there will be no problem if the query is continuous. If the query pauses for more than wait_timeout after several queries, the database connection will be lost and the database connection will be lost.
Reproduction
Use Django to reproduce the problem next time:
1. Set the wait_timeout of mysql to 10 seconds, and then enter the django shell simulation Query (only part of the following error message is retained)
In[1]:import time In[2]:from django.contrib.auth.models import User In[3]:list(User.objects.filter(id=1)) Out[3]:[<user:>] In[4]:time.sleep(15) # 模拟比较慢的代码(其中没有查询数据库的代码),或者空闲什么都不操作一段时间,此时间要比`wait_timeout`大一些 list(User.objects.filter(id=1)) Traceback (most recent call last): File "<ipython-input-4-3574ae8220ee>", line 1, in <module> list(User.objects.filter(id=1)) File "/usr/lib/python3.6/site-packages/pymysql/connections.py", line 1037, in _read_bytes CR.CR_SERVER_LOST, "Lost connection to MySQL server during query") django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')</module></ipython-input-4-3574ae8220ee></user:>
Seeking
Then the above problem basically shows that the error is caused by too long idle time.
In order to reduce unnecessary database connections and closures, Django reuses database connections. When a request is started, a connection pool is established to store the connection. After that, a connection is reused for each request. My guess is that Django saves the connection longer than wait_timeout. If the save time is shorter, the connection can be re-established to avoid this error.
Yes, the official document has also explained this problem, setting the database CONN_MAX_AGE parameter, example:
DATABASES = { "default": { 'ENGINE': 'django.db.backends.mysql', 'NAME': '', 'USER': '', 'PASSWORD': '', 'HOST': '', 'CONN_MAX_AGE': 9 # 比wait_timeout小一些 } }
When we tested it, we found that things were not as simple as we thought. Why does the error still occur? Behind all this, is it the distortion of human nature or the loss of morality? Please watch the next episode "Breakthrough".
Breakthrough
After searching CONN_MAX_AGE
in the django source code, I followed the clues and found out how django closes the failed connectiondjango. db.close_old_connections()
:
# Register an event to reset transaction state and close connections past # their lifetime. def close_old_connections(**kwargs): for conn in connections.all(): conn.close_if_unusable_or_obsolete() signals.request_started.connect(close_old_connections) signals.request_finished.connect(close_old_connections)
The focus is on the last two lines. This method is executed when specific events are implemented through signal. The two specific events, as the name implies, are the start of the request and the end of the request. The error we reported was in one request, so this method is usually ineffective. It only closes and re-establishes the connection for each request.
Solution
Do not close the django shell that reproduces the problem. Continue to execute the following code:
In[5]:from django.db import close_old_connections In[6]:close_old_connections() In[7]:list(User.objects.filter(id=1)) Out[7]: [<user:>]</user:>
Call django.db.close_old_connections and query again. No more errors.
Then if we want to avoid this error, we must call the django.db.close_old_connections method before executing each database query.
1. Generally, this kind of problem will not occur, because the database query is continuously performed in one request, and there is no need to call this method for every request. This is a groundless worry.
2. Sometimes there is a large amount of data in a request, and the database will be queried and then other processing (not involving the database) will be performed for a period of time. For example, some data will be queried first, then the data will be processed, excel will be generated, the file will be saved, and Generate url. It is known that this takes a very long time, so it is best to call django.db.close_old_connections first to prevent the connection from being lost when the final URL is saved in the database.
The above is the detailed content of Solving the problem of Django database connection loss (explanation with examples). For more information, please follow other related articles on the PHP Chinese website!

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
