2

正常に機能する長時間実行プロセスがあります。手動でトランザクションをコミットする transaction.commit() を追加した後、(2006 'MySQL server has gone away') エラーが発生し始めました。

前(素晴らしい作品):

DBObject.objects.get(id = 1)

後:(夜間に処理するものが何もない状態で8時間アイドル状態になった後にエラーが発生する)

注: 古いデータを取得しないようにするには、このようにフラッシュする必要があります。

flush_transaction()
DBObject.objects.get(id = 1)

どこ

@transaction.commit_manually
def flush_transaction():
    """
    Flush the current transaction so we don't read stale data

    Use in long running processes to make sure fresh data is read from
    the database.  This is a problem with MySQL and the default
    transaction mode.  You can fix it by setting
    "transaction-isolation = READ-COMMITTED" in my.cnf or by calling
    this function at the appropriate moment
    """
    transaction.commit()

私が理解しているように、私は commit_manually に切り替えていますが、django の自動再接続も失っているようです。

mysql側でwait_timeoutを増やす以外に、これを処理する良い方法はありますか?

4

1 に答える 1

1

問題は commit_manual モードへの切り替えにありました。接続を適切に閉じていないようです。django とデータベース接続の詳細をお読みください。

問題を解決したのは、データベース接続を手動で閉じて、クエリを再試行することです。

使用した接続を手動で閉じるには

from django import db
db.close_connection()

これが役に立ったことを願っています

于 2013-09-04T00:22:02.417 に答える