title: Django: how to get a fresh connection to the database
body: <p>I have built a web server with django. </p>
  
  <p>And use a thread to run task, which save data sent from clients to the database.</p>
  
  <blockquote>
  <pre><code>  while True:
        ...
        try:  
            self.dbTarget, created = ClientMonitor.objects.get_or_create(Machine_code=own_MachC)
  
            data = self.workQueue.get()  #from client sent
  
            self.dbTarget.setValue(data) #Custom method assigning
            self.dbTarget.save()         #django ORM
        except InterfaceError as ee:
            pass
  </code></pre>
  </blockquote>
  
  <p>The thread is long-running, but it will except a InterfaceError after a long time because the mysql server disconnect as eight-hours timeout.</p>
  
  <blockquote>
    <p>Especially it can not auto-reconnect to the db. Creating a new thread will be OK, but it will increase resource occupation.</p>
  </blockquote>
  
  <p>So I want to reconnect db when connection closed in the same thread. Also Django each request gets a fresh connection.</p>
  
  <p>How I can get a fresh connection to the database in the same thread?
  Thank you!!!</p>