0
  • From django db doc , when use persistent connection(set CONN_MAX_AGE > 0 or None), it won't be closed after each request util it exceeds the maximum age or when it isn’t usable any longer.
  • That's being said, the following reuqests will use the same persistent connetion that has being created before, which may cause one persistent connection shared by multithread at the same time. But this situation shouldn't happen! I'm just confused and I believe django developers won't make such shallow mistake. Or do I miss something?

    Thanks in advance!

Dick Smith
  • 11
  • 4

1 Answers1

0

Well, after searching and read django source code, I indeed miss some points! The 'trick' django avoid sharing one persistent db connetion across threads is using threading.local() in ConnectionHandler(path: django/db/utils.py) when instantiated.

In this way, each thread maintains its own persistent connection. How clever! And I also draw flow diagram: django db connetion processing

Hope to help others!

Dick Smith
  • 11
  • 4