Django model layer (database) connection processing logic

created at 07-17-2021 views: 1

Django model layer (database) connection processing logic:

  1. Use CONN_MAX_AGE configuration to limit the DB connection life
  2. The default value of CONN_MAX_AGE is 0
  3. The lifetime of each DB connection is maintained until the end of the request

Disadvantages of the default database connection

  1. Each request will repeatedly connect to the database
  2. Processing high concurrent requests puts a lot of pressure on the service
  3. Cannot host high-concurrency services

How to avoid django over-optimization

Store database connection location: thread local variables (when django is deployed, each thread processed has a database connection, because this connection is saved in thread local variables and is not affected by other threads)

The maximum number of connections supported by the mysql database, (if the number of deployment threads exceeds the maximum number of connections to the database, it will cause other services to connect to mysql incorrectly, resulting in failure to connect to the database)

How to view the maximum number of mysql connections:

Use Navicat, find the corresponding database, right click, find the command line interface, enter the command:

mysql>  show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set

mysql>

You can know that the maximum number of connections to the mysql database is 151.

Modify the maximum number of mysql connections:

mysql>  set global max_connections =1000;

Query OK, 0 rows affected

mysql>  show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 1000  |
+-----------------+-------+
1 row in set

mysql>
Please log in to leave a comment.