我已經用django構建了一個web服務器。Django:如何獲得與數據庫的新連接
並使用線程來運行任務,該任務保存從客戶端發送到數據庫的數據。
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
線程是長期運行,但時間長了以後,會除外InterfaceError因爲MySQL服務器斷開連接八個小時的超時。
Especially it can not auto-reconnect to the db. Creating a new thread will be OK, but it will increase resource occupation.
所以我要重新連接數據庫時,連接在同一個線程關閉。此外,Django每個請求都會獲得全新的連接。
如何在同一個線程中獲得與數據庫的新連接? 謝謝!!!
我可以調用'connection.connection.close()'來模擬'InterfaceError'。但這可能是不同的。 – Mix