Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django issue fix: orm - too many connection #19

Open
hhstore opened this issue May 1, 2018 · 2 comments
Open

django issue fix: orm - too many connection #19

hhstore opened this issue May 1, 2018 · 2 comments
Labels

Comments

@hhstore
Copy link
Owner

hhstore commented May 1, 2018

问题:

  • django 大并发下, 数据库连接数超过上限.

fix:

  • 设置单个连接的存活时间为默认值0, 不要轻易修改.
  • 使用 aws 的 RDS集群, aws 自身已经对 MySQL做了集群优化, db connection 本身是有连接池的.

CONN_MAX_AGE=0
CONN_MAX_AGE=60

ref:

@hhstore hhstore added the Django label May 1, 2018
@hhstore
Copy link
Owner Author

hhstore commented May 1, 2018

django 问题源码分析:

  • 基于 django 1.11

  • /django/db/utils.py

  • django.db.utils.ConnectionHandler#ensure_defaults()

image

  • django.db.backends.base.base.BaseDatabaseWrapper#connect()
    • 关键处, 默认连接存活时间为0.
      image

image

  • 调用链:
  • django.db.backends.base.base.BaseDatabaseWrapper#close_if_unusable_or_obsolete()

image

  • 继续:
  • django.db.backends.base.base.BaseDatabaseWrapper#close()

image

  • 继续:
  • django.db.backends.base.base.BaseDatabaseWrapper#_close()

image

@hhstore
Copy link
Owner Author

hhstore commented May 1, 2018

源码分析结论:

  • django 1.11 默认CONN_MAX_AGE = 0, 对每个 HTTP request 结束后, 自动close()掉db连接. 默认做法更安全. 防止多线程下出问题.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant