Django how to reconnect after DatabaseError: query timeout

前端 未结 1 754
太阳男子
太阳男子 2021-02-04 07:51

I have a stand-alone script that reads/writes from/to Postgre using Django ORM.

I get this error occasionally

DatabaseError: query timeout server

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 08:38

    I have a similar need for recreating the database connection and I'm trying the following black magic to reset the connection in django 1.3:

    from django.db import connection
    connection.connection.close()
    connection.connection = None
    

    I don't have PostgreSQL handy to try this out, but it seems to work for MySQL and sqlite at least. Also, if you're using multi-db, you're going to have to perform this step on your specific connection from the django.db.connections dictionary.

    0 讨论(0)
提交回复
热议问题