"""Connection hygiene for long work between queries (renders): the database may close an idle connection."""

from __future__ import annotations

from django.db import connection


def revive_connection() -> None:
    """Drop a connection the server has closed, so the next query opens a fresh one.

    Safe only outside a transaction; inside one the broken connection must surface as an error.
    """
    if connection.in_atomic_block or connection.connection is None:
        return
    if not connection.is_usable():
        connection.close()
