Backport: Implement pessimistic connection invalidation (MySQL only)

Backport of #700 to `4.0-stable` branch.

@phgross