[1.7.x] Fixed #21553 -- Ensured unusable database connections get closed.

Backport of 5f2f47f from master